题目29:设定一个用户名和一个密码,用户输入正确的用户名和密码,则显示登录成功,否则显示登录失败。
用户最多失败3次,否则退出程序。
提示:使用while或者for来限定重试次数,使用input获取用户输入使用==判断用户的用户名和密码。
方法1for:
user_name_in_system="root"
user_passwd_in_system="root123"
for i in range(3):
user_name_input=input("please input username:")
user_passwd_input=input("please input userpasswd:")
if user_name_input==user_name_in_system and \
user_passwd_input==user_passwd_in_system:
print("login successfully!")
break
else:
print("wrong username or password!")
else:
print("input times is used out! bye!")
user_name_in_system="root"
user_passwd_in_system="root123"
for i in range(3):
user_name_input=input("please input username:")
user_passwd_input=input("please input userpasswd:")
if user_name_input==user_name_in_system and \
user_passwd_input==user_passwd_in_system:
print("login successfully!")
break
else:
print("wrong username or password!")
if i==2:
print("input times is used out! bye!")
方法2(while):
user_name_in_system="root"
user_passwd_in_system="root123"
i=2
while i>=0:
user_name_input=input("please input username:")
user_passwd_input=input("please input userpasswd:")
if user_name_input==user_name_in_system and \
user_passwd_input==user_passwd_in_system:
print("login successfully!")
break
else:
print("wrong username or password!")
if i==0:
print("input times is used out! bye!")
i-=1
版权声明:本文为sun_qian_li原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。