标签:退出 信息 fail pre 三次 事先 rip too 扩展
不要急于马上把三次登陆写出来,一定要将复杂的程序简单化,必须一步一步地去扩展,这样才保证不会出错。
# 事先定义 user = ‘dark_knight‘ pwd = ‘dk123‘ username = input(‘Please enter username:‘) password = input(‘Please enter password:‘) # 判断 if username == user and password == pwd: print(‘Login successfully!‘) else: print(‘Login failed!‘)
# 事先定义 user = ‘dark_knight‘ pwd = ‘dk123‘ count = 1 # 定义次数 while True: if count == 4: # 大于3次则退出循环 print(‘Too many times!‘) break username = input(‘Please enter username:‘) password = input(‘Please enter password:‘) # 判断 if username == user and password == pwd: print(‘Login successfully!‘) break else: print(‘Login failed!‘) count += 1 # 每次循环都次数都进行加1操作
1、去除字符串两边的空格
2、当用户输入None时则提示用户反复输入。
3、当用户输入的信息不匹配时则告诉用户还有几次机会。
# 事先定义 user = ‘dark_knight‘ pwd = ‘dk123‘ count = 1 # 定义次数 while True: if count == 4: # 大于3次则退出循环 print(‘Too many times!‘) break username = input(‘Please enter username:‘).strip() # 去除字符串两边的空格 password = input(‘Please enter password:‘).strip() # 去除字符串两边的空格 # 当用户输入有值时 if username and password: # 判断 if username == user and password == pwd: print(‘Login successfully!‘) break else: print(‘Login failed!‘) print(‘You have %s chance.‘%(3 - count)) # 当用户输入None时 else: print(‘You enter blank, please re-enter!‘) continue # 当用户输入None时,跳出本次循环。 count += 1 # 每次循环都次数都进行加1操作
标签:退出 信息 fail pre 三次 事先 rip too 扩展
原文地址:http://www.cnblogs.com/fyknight/p/7967436.html