周末作业 3/15

二:周末综合作业:
2.1:编写用户登录接口
1、输入账号密码完成验证,验证通过后输出"登录成功"
2、可以登录不同的用户
3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)

用户信息:user.txt
egon:532:0
tank:123:0
wg:123:0

文件处理之豪华升级版

import time, os

tag = 1
d = {}
while True and tag:
    user = input('请输入姓名:')
    pwd = input('请输入密码:')
    with open('users.txt', 'r+', encoding='utf-8') as f:
        l = f.read()
        f.seek(0)
        for i in f:
            user1, pwd1, count = i.strip().split(':')
            if user == user1:
                if os.path.exists('{}.txt'.format(user)):
                    with open('{}.txt'.format(user),'r',encoding= 'utf-8') as f2:
                        l1 = f2.read()
                        l1 = float(l1)
                        print(l1)
                    if l1 < time.time():
                        lll = l.replace('{}:{}:{}'.format(user1, pwd1, count), '{}:{}:0'.format(user1, pwd1))
                        f.seek(0)
                        f.write(lll)
                        os.remove('{}.txt'.format(user))
                    else:
                        print('用户被锁定')
                        break
                if pwd == pwd1:
                    print('登陆成功')
                    tag = 0
                    break
                else:
                    f.seek(0)
                    l = f.read()
                    count = int(count)
                    count_1 = count + 1
                    ll = l.replace('{}:{}:{}'.format(user1, pwd1, count), '{}:{}:{}'.format(user1, pwd1, count_1))
                    f.seek(0)
                    print(ll)
                    f.write(ll)
                    print(ll)
                    print('登陆失败')
                if count == 2:
                    with open('{}.txt'.format(user), 'w', encoding='utf-8') as f1:
                        f1.write(str(time.time() + 300))
                    print('用户被锁定了')
                break
        else:
            print('用户名不正确哦')

2.2:编写程序实现用户注册后,可以登录,
提示:

msg = """0 退出
1 登录
2 注册
    """
while True:
    print(msg)
    cmd = input('请输入命令编号>>: ').strip()
    if not cmd.isdigit():
        print('必须输入命令编号的数字,傻叉')
        continue
    if cmd == '0':
        break
    elif cmd == '1':
        # 登录功能代码(附加:可以把之前的循环嵌套,三次输错退出引入过来)
        while True:
            user = input('请输入账号:').strip()
            pwd = input('请输入密码:').strip()
            with open('users.txt',mode = 'r',encoding = 'utf-8') as f:
                for line in f:
                    if line:
                        user1, pwd1, count = line.strip().split(':')
                        if user == user1 and pwd == pwd1:
                            print('登录成功')
                            break
                else:
                    print('登陆失败,用户名或密码错误')
            break
    elif cmd == '2':
        # 注册功能代码
        with open('users.txt','r',encoding='utf-8') as f5:
            l = f5.read()
        ll = l.split('
')
        print(ll)
        l_user = []
        for i in ll:
            lll = i.split(':')
            print(lll)
            l_user.append(lll[0])
            print(l)
        while True:
            user = input('请输入账号:').strip()
            if user:
                if user not in l_user:
                    pwd = input('请输入密码:').strip()
                    if pwd:
                        pwd1 = input('请再次输入确认密码:').strip()
                        if pwd == pwd1:
                            with open('users.txt',mode = 'a',encoding = 'utf-8') as f:
                                f.write('
{}:{}:0'.format(user,pwd))
                            print('注册成功')
                            break
                        else:
                            print('密码不一致')
                    else:
                        print('密码不能为空')
                else:
                    print('用户名被占用了')
            else:
                print('用户名格式不正确')
    else:
        print('输入的命令不存在')

思考:上述这个if分支的功能否使用其他更为优美地方式实现

答:用while,,,?

原文地址:https://www.cnblogs.com/pythonwl/p/12499930.html