二、周末综合作业:

2.1:编写用户登录接口

1、输入账号密码完成验证,验证通过后输出"登录成功"

2、可以登录不同的用户

3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,

这样才能保证程序关闭后,该用户仍然被锁定)

# with open(r"lock_list.txt",mode="a", encoding="utf-8") as f:
#     pass
# with open(r"user_info.txt",mode="a", encoding="utf-8") as f:
#     f.write("wu:123\n")
#     f.write("wuu:222\n")
login = 1
while login:
    inp_user = input("请输入账号").strip()
    inp_psw = input("请输入密码").strip()
    login_sign = 1
    # func:同一账号输错三次锁定
    with open(r"lock_list.txt", \
              mode="r", encoding="utf-8") as f:
        count = 0
        for user in f:
            if user.strip() == inp_user:
                count += 1
                if count > 2:
                    print("该账号被锁定,不能登陆")
                    login_sign = 0
                    break
    if login_sign:
        with open(r"user_info.txt", \
                  mode="r", encoding="utf-8") as f:
            for line in f:
                user, password = line.strip().split(":")
                if inp_user == user and inp_psw != password:
                    print("密码错误,请重新输入")
                    with open(r"lock_list.txt",\
                              mode="a", encoding="utf-8") as f:
                        f.write("{}\n".format(inp_user))
                    break
                elif inp_user == user and inp_psw == password:
                    print("登陆成功")
                    login = 0
                    break
            else:
                print("登录失败,账号不存在")
print("下一步")

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

msg = """
0 退出
1 登录
2 注册
"""
with open(r"lock_list.txt", mode="a", encoding="utf-8") as f:
    f.write("")
with open(r"user_info.txt", mode="a", encoding="utf-8") as f:
    f.write("")
while True:
    print(msg)
    cmd = input('请输入命令编号>>: ').strip()
    if not cmd.isdigit():
        print('必须输入命令编号的数字')
        continue

    elif cmd == '0':
        break
    elif cmd == '1':
        login = 1
        while login:
            inp_user = input("请输入账号,如想返回上一层输入Q").strip()
            if inp_user.upper() == "Q":
                break
            inp_psw = input("请输入密码").strip()
            login_sign = 1
            # func:同一账号输错三次锁定
            with open(r"lock_list.txt", \
                      mode="r", encoding="utf-8") as f:
                count = 0
                for user in f:
                    if user.strip() == inp_user:
                        count += 1
                        if count > 2:
                            print("该账号被锁定,不能登陆")
                            login_sign = 0
                            break
            # 查看是否锁定
            if login_sign:
                # func:账号密码验证
                with open(r"user_info.txt", \
                          mode="r", encoding="utf-8") as f:
                    for line in f:
                        user, password = line.strip().split(":")
                        if inp_user == user and inp_psw != password:
                            print("密码错误,请重新输入")
                            with open(r"lock_list.txt", \
                                      mode="a", encoding="utf-8") as f:
                                f.write("{}\n".format(inp_user))
                            break
                        elif inp_user == user and inp_psw == password:
                            print("登陆成功")
                            while 1:
                                cmd = input("请输入命令:     Q注销")
                                if cmd.upper() == "Q":
                                    break
                                else:
                                    print("命令{}正在执行".format(cmd))
                            login = 0
                            break
                    else:
                        print("登录失败,账号不存在")
    elif cmd == '2':
        inp_user = input("请输入想注册的账号")
        inp_psw = input("请输入密码")
        with open(r"user_info.txt", \
                  mode="r", encoding="utf-8") as f:
            for line in f:
                user = line.strip().split(":")[0]
                if inp_user == user:
                    print("账号名已存在,不能注册")
                    break
            else:
                with open(r"user_info.txt", \
                          mode="a", encoding="utf-8") as f:
                    f.write("{}:{}\n".format(inp_user, inp_psw))
                print("注册成功")
    else:
        print('输入的命令不存在')

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


 posted on 2020-03-13 20:34  wwwpy  阅读(185)  评论(0编辑  收藏  举报