注册 登录
编程论坛 Python论坛

【求助】登陆代码变死循环了求大神指正

fuximobei 发布于 2019-03-20 19:21, 1322 次点击
#实现注册和登陆功能,并储存用户信息,登陆三次失败将锁定账号
#存储库
user_info={'Alex':123468,'Betty':233546}
lock_info=[]
#输入信息
choice=input('新用户:0/老用户:1\t')
username=input('请输入用户名:')
password=input('请输入密码:')
#计数器
count=0
#验证函数
def user_exist():#新用户注册,验证用户名是否存在
    if username in user_info:
        print('此用户名已存在,请重新注册')
    else:
        user_info.update({username:password})
        print('欢迎新用户%s来到完美世界'%(username))
    return
def user_lock():
    if username in lock_info():
        print('用户名已被锁定,请明天再试')
    return
def user_not_exist():
    if username not in user_info():
        print('此用户不存在,请重新尝试')
#循环语句登陆三次
while count<3:
    if choice=='0':
        user_exist()
    elif choice=='1':
        user_not_exist()
萌新求助,要写一个注册登陆程序,老用户登陆,新用户注册并保存新用户信息,登陆超过三次锁定账户。不知道函数哪里出了问题一测试就死循环了,求大神看下代码哪里出了问题,多谢
1 回复
#2
TysonKoothra2019-03-23 21:38
while里面应该写上count+=1
所有代码,我是这样写的。
程序代码:

users_info = {
    "Alex": {"password": 123468, "wrong_times": 0},
    "Betty": {"password": 233546, "wrong_times": 0},
}  # 用户账号密码信息


def user_lock():
    print("用户名已被锁定,请明天再试")


def login(username, passwd):
    if username in users_info:
        if users_info[username]["wrong_times"] >= 3:
            user_lock()
            return -1

        if passwd == users_info[username]["password"]:
            print("You have logged in.")
            users_info[username]["wrong_times"] = 0
            return 1
        else:
            print("Wrong password, log in again")
            users_info[username]["wrong_times"] += 1
            return 0

    else:
        print("此用户不存在,请重新尝试")
        return 2


def register(username, passwd):
    if username not in users_info:
        users_info.update({username: {"password":passwd, "wrong_times":0}})
        print("欢迎新用户%s来到完美世界" % (username))
        return 1
    else:
        print("用户%s已存在,请使用新账号名。" % (username))
        return 0


if __name__ == "__main__":
    choice = 0
    while choice == 0 or choice == 1:
        choice = int(input("新用户:0/老用户:1/其他数字退出程序\t"))
        username = input("请输入姓名:")
        passwd = input("请输入密码:")
        if choice == 0:
            register(username, passwd)
        elif choice == 1:
            login(username, passwd)

1