作业3.15(周末作业)

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

count=0
dic={
    'zc':123,
    'zc1':123,
    'zc2':123,
}
while count<3:
    name=input('请输入你的账号:').strip()
    if name in dic.keys():
        pwd = input('请输入你的密码:').strip()
        if pwd in dic.values():
            print('登陆成功')
            break
        else:
            print('账号或密码错误!')
            count+=1
            if count==3:
                with open('a.txt',mode='r+t',encoding='utf-8') as f1:
                    f1.write('{}'.format(name))
                    print('您的账号已锁定')
                    break

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

while True:
    msg = """
    0 退出
    1 登录
    2 注册
    """
    print(msg)
    print('您是否已经注册,如果没有请先注册登录')
    cmd = input('请输入命令编号>>: ').strip()
    if not cmd.isdigit():
        print('必须输入命令编号的数字,傻叉')
        continue

    if cmd == '0':
        break
    elif cmd == '1':
        count=0
        while count<3:
        # 登录功能代码(附加:可以把之前的循环嵌套,三次输错退出引入过来)
            inp_username=input('your name>>: ').strip()
            inp_password=input('your password>>: ').strip()

            # 验证
            with open('a.txt',mode='rt',encoding='utf-8') as f:
                for line in f:
                    username,password=line.strip().split(':')
                    if inp_username == username and inp_password == password:
                        print('login successfull')
                        break
                else:
                    print('账号或密码错误')
                    count+=1
    elif cmd == '2':
        # 注册功能代码

        while True:
            username1 = input('请输入你要注册的用户名:').strip()
            password1 = input('请输入你要注册的密码:').strip()
            with open('a.txt', mode='r+t', encoding='utf-8') as f:
                for line1 in f:
                    username2, password2 = line1.strip().split(':')
                    if username1==username2:
                        print('用户已注册!')
                        continue
                    else:
                        f.write('{}:{}\n'.format(username1, password1))
                        print('恭喜您,注册成功')
                        break
            break
    else:
        print('输入的命令不存在')
posted @ 2020-03-15 17:55  小子,你摊上事了  阅读(95)  评论(0)    收藏  举报