python--第四天练习题

Posted fan-gx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python--第四天练习题相关的知识,希望对你有一定的参考价值。

#1、写函数,利用递归获取斐波那契数列中的第 10 个数,并将该值返回给调用者。
def rec(a,b,dep=1):
    c = a + b
    if dep == 10:
        return c
    return rec(b,c,dep+1)

print(rec(5,5))


#2、编写用户登录程序,(读写文本版,可注册用户,修改用户密码)
import os
#注册账号
def zhuce(user,pawd,file):
    with open(file, 'r+', encoding='utf-8') as f1:
        for line in f1:
            if user == line.split(':')[0]:
                return False
        else:
            f1.write(user + ':' + pawd + '\n')
            return True
#登录账号
def denglu(user,pawd,file):
    with open(file, 'r', encoding='utf-8') as f1:
        for line in f1:
            if ':'.format(user,pawd) == ''.join(line).strip():
                return True
        return False

#修改密码
def xiugai(user,pawd,file,new_pawd):
    line2 = ''
    ret = False
    with open(file,'r') as f1,open('fana.bak','w') as f2:
        for line in f1:
            if ':'.format(user,pawd) == line.strip():
                ret = True
                line2 += line.replace(user + ':' + pawd, user + ':' + new_pawd)
            else:
                line2 +=line
                if ret != True:
                    ret = False
        f2.write(line2)
    os.remove('fana.txt')
    os.rename('fana.bak','fana.txt')
    return ret

#注销用户
def zhuxiao(user,pawd,file):
    line2 = ''
    ret = False
    with open(file,'r') as f1,open('fana.bak','w') as f2:
        for line in f1:
            if ':'.format(user,pawd) == line.strip():
                ret = True
            else:
                line2 += line
                if ret != True:
                    ret = False
        f2.write(line2)
    os.remove('fana.txt')
    os.rename('fana.bak','fana.txt')
    return ret

def taobao(arg):
    file = 'fana.txt'
    user = input("请输入用户名:")
    pawd = input("请输入密码:")
    if arg == 'z':
        zhu = zhuce(user,pawd,file)
        if zhu == False:
            print("用户名已存在")
        else:
            print('注册成功')
    elif arg == 'a':
        deng = denglu(user,pawd,file)
        if deng:
            return '欢迎登陆淘宝!'
        else:
            print('账号或密码错误')
    elif arg == 'x':
        new_pawd = input("请输入您的新密码")
        xiu = xiugai(user,pawd,file,new_pawd)
        if xiu:
            print("密码修改成功")
        else:
            print("账号或密码错误")
    elif arg == 'e':
        xiao = zhuxiao(user,pawd,file)
        if xiao:
            print("账号注销成功")
        else:
            print("账号或密码错误")

while True:
    jd = input("注册请按:z,登陆请按:a,退出按:q >>")
    if jd.lower() == 'z':
        taobao('z')
    elif jd.lower() == 'a':
        ta = taobao('a')
        if ta == '欢迎登陆淘宝!':
            print('欢迎登陆淘宝!')
            se = input('修改密码:x,注销:e >>')
            if se == 'x':
                print('修改密码前!',end='')
                taobao('x')
            elif se == 'e':
                print('注销账号前!', end='')
                taobao('e')
            else:
                print('请按照提示输入!')
    elif jd.lower() == 'q':
        print('退出成功')
        break
    else:
        print("请按照提示输入!")

以上是关于python--第四天练习题的主要内容,如果未能解决你的问题,请参考以下文章

python学习之第十四天补充

第四天的练习

第四天-代码练习

算法练习-第四天(链表中倒数最后k个结点)

算法练习-第四天(链表中倒数最后k个结点)

算法练习-第四天(链表中倒数最后k个结点)