1 # -*- coding: utf-8 -*- 2 # 基础需求 3 """ 4 让用户输入用户名密码 5 认证成功后显示欢迎信息 6 输错三次退出程序 7 """ 8 _username = "alice" 9 _password = "123" 10 count = 0 11 12 while count < 3: 13 username = input("Username:") 14 password = input("Password:") 15 if username == _username and password == _password: 16 print("登录成功!") 17 break 18 else: 19 print("登录失败!") 20 count += 1
1 # -*- coding: utf-8 -*- 2 # 升级需求 3 """ 4 可以支持多个用户登录 5 用户3次认证失败后,退出程序,再次启动程序尝试登录时,是锁定状态 6 需把锁定状态存到文件中 7 """ 8 import os 9 _listname = ['alice', 'alex', 'lily'] 10 _password = "123" 11 count = 0 12 while count < 3: 13 if os.path.exists('lock_file.txt'): 14 f = open('lock_file.txt', 'r', encoding='utf-8') 15 if '3次' in f.read(): 16 print('用户已锁定') 17 f.close() 18 break 19 username = input("Username:") 20 password = input("Password:") 21 if password == _password: 22 if not username in _listname: 23 print("登录失败!") 24 else: 25 print("登录成功!") 26 break 27 else: 28 print("登录失败!") 29 count += 1 30 else: 31 print('失败', '次数', '3次', sep='->', end=' ', file=open('lock_file.txt', 'w', encoding='utf-8'))