写一个函数完成三次登陆功能:
用户的用户名密码从一个文件register中取出。
register文件包含多个用户名,密码,用户名密码通过|隔开,每个人的用户名密码占用文件中一行。
完成三次验证,三次验证不成功则登录失败,登录失败返回False。
登陆成功返回True。
def login(): count = 0 while count < 3: sur = input(‘username:‘) pwd = input(‘password:‘) with open(‘refister‘,mode=‘r‘,encoding=‘utf-8‘) as f: for line in f: line = line.strip() user,passwd = line.split(‘|‘) if user == sur and passwd == pwd: return True count +=1 print(‘登陆失败,请重新登陆‘) return False ret = login() if ret: print(‘登陆成功‘) else: print(‘三次登陆机会已用尽,登陆失败‘)
原文:https://www.cnblogs.com/wanglan/p/9929097.html