10 月 102020
 
password_list = ['*#*#', '12345']
def account_login():
    tries = 3 #赋值变量,初始密码重试次数为3
    while tries > 0: #while循环
        password = input('Password:') #用户输入赋值变量
        #密码为12345,则password_correct = True
        password_correct = password == password_list[-1] 
        #密码为*#*#,则password_reset = True
        password_reset = password == password_list[0]

        if password_correct: #密码正确,打印成功提示,继续while循环
            print('Login success!')
        elif password_reset: #重设密码正确,提示输入新密码
            new_password = input('Enter a new password:')
            password_list.append(new_password) #新密码写入列表
            print('Password has changed successfully!')
            account_login() #重新调用函数登录
        else: #反之,提示错误
            print('Wrong password or invalid input!')
            tries = tries - 1 #同时对重试次数减1,并重新赋值给tries变量
            print(tries, 'times left') #打印提示剩余可重试次数
    else: #当while循环为False时,即tries不大于0时,打印提示账户锁定
        print('Your account has been suspended')
        exit() #break只可以用在while和for循环中,在超过重试次数后退出

#调用函数
account_login()
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe c:/Users/harveymei/hello.py
Password:12345
Login success!
Password:*#*#
Enter a new password:54321
Password has changed successfully!
Password:123
Wrong password or invalid input!
2 times left
Password:123
Wrong password or invalid input!
1 times left
Password:123
Wrong password or invalid input!
0 times left
Your account has been suspended
PS C:\Users\harveymei>

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)