博客已经搬家到“捕获完成”:
https://www.v2python.com
1、while
2、
3、改进的例子,并添加了break,跳出
# -*- coding: UTF-8 -*-
# 千万要记住要把这个初始的列表放到函数体外
passwd = ['change', '123456']
def account_login():
tries = 3 #增加了尝试次数tries
while tries > 0 : #这里增加了 while :
input_passwd = input('Please input the passwd:\n')
if input_passwd == passwd[-1]:
print('Right,login\n')
break #登陆成功,跳出
elif input_passwd == passwd[0]:
reset_passwd = input('ok,u will reset passwd:\n')
passwd.append(reset_passwd)
print('Right,password changed, new password is:{} \n'.format(passwd[-1])) # 对这一句话进行了修改,添加了{}
account_login()
else:
tries = tries -1
print('Wrong, Re-input password,and u have {} times left\n'.format(tries))
else: #这里增加了else:
print('Your account has been suspended')
print('Welcome !\n')
account_login()