作业day08

2020-06-10

作业一:编写用户登录接口
1、输入账号密码完成验证,验证通过后输出"登录成功"

while True:
    inp_name = input("请输入您的用户名:").strip()
    inp_psw = input("请输入您的密码:").strip()
    if inp_name == "jason" and inp_psw == "123":
        print("登录成功!")
        break
    else:
        print("登录失败!请重新输入")

2、可以登录不同的用户

i = 0
while i < 3:
    inp_name = input("请输入您的用户名:").strip()
    inp_psw = input("请输入您的密码:").strip()
    f = open(r'registration.txt', mode='rt', encoding='utf-8')
    for line in f:
        username, userpsw = line.strip('
').split(':')
        if inp_name == username and inp_psw == userpsw:
            print("登录成功!")
            i = 4
            break
    else:
        print("登录失败,请重新输入!")
        i += 1
    f.close()

*选做功能:同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)


作业二:编写程序实现用户注册后(注册到文件中),可以登录(登录信息来自于文件)
提示:
while True:
  msg = """
  0 退出
    1 登录
    2 注册
    """
print(msg)
cmd = input('请输入命令编号>>: ').strip()
if not cmd.isdigit():
print('必须输入命令编号的数字,傻叉')
continue

if cmd == '0':
break
elif cmd == '1':
# 登录功能代码(附加:可以把之前的循环嵌套,三次输错退出引入过来)
pass
elif cmd == '2':
# 注册功能代码
pass
else:
print('输入的命令不存在')

while True:
    msg = """
    0 退出
    1 登录
    2 注册
    """
    print(msg)
    cmd = input('请输入命令编号>>: ').strip()
    if not cmd.isdigit():
        print('必须输入命令编号的数字')
        continue
    if cmd == '0':
        break
    elif cmd == '1':  # 登录程序
        i = 0
        while i < 3:
            inp_name = input("请输入您的用户名:").strip()
            inp_psw = input("请输入您的密码:").strip()
            f = open(r'registration.txt', mode='rt', encoding='utf-8')
            for line in f:
                username, userpsw = line.strip('
').split(':')
                if inp_name == username and inp_psw == userpsw:
                    print("登录成功!")
                    i = 4
                    break
            else:
                print("登录失败,请重新输入!")
                i += 1
            f.close()
    elif cmd == '2':  # 注册程序
        tag = True
        while tag:
            inp_name = input("请输入您的用户名:").strip()
            f = open(r'registration.txt', mode='rt', encoding='utf-8')
            for line in f:
                username2, *_ = line.strip('
').split(":")
                if inp_name != username2:
                    while tag:
                        inp_psw = input("请输入您的密码(仅由数字和字母组成):").strip()
                        if inp_psw.isalnum():
                            f1 = open(r'registration.txt', mode='at', encoding='utf-8')
                            f1.write("%s:%s
" % (inp_name, inp_psw))
                            f1.close()
                            tag = False
                        else:
                            print("您输入的密码格式有误,请重新输入")
                else:
                    print("用户名已存在,请重新输入")
                    continue
            f.close()
    else:
        print('输入的命令不存在')
原文地址:https://www.cnblogs.com/cui-cheng/p/13090002.html