模拟登陆:
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
方法一 (分别输入姓名和密码)
1 #账号和密码 2 name = 'nikita' 3 password = '123' 4 count = 0 5 #读取黑名单 6 f = open('black_list.txt','r') 7 black = f.read() 8 f.close() 9 ls = [] 10 #三次登录信息 11 T = True 12 while T: 13 name1 = input('Please enter name:') 14 #检查name1是否在black_list 15 16 if name1 in black: 17 print ('Sorry, you cannt login') 18 break 19 #不在black_list 20 else: 21 password1 = input("Please enter password:") 22 if name == name1 and password == password1: 23 print ('Welcome!') 24 break 25 26 else: 27 print ('Your name and password are wrong') 28 ls.append(name1) 29 x = ls.count(name1) #输入的名字放入list里,当相同名字输入三次,锁定账号 30 if x>=3: 31 T = False 32 print('Your account is locked.') 33 f = open('black_list.txt','a') 34 f.write(name1 + ' ') 35 f.close() 36 break
方法二(先输入名字,在输入多次密码)
1 #账号和密码 2 name = 'nikita' 3 password = '123' 4 count = 0 5 #读取黑名单 6 f = open('black_list.txt','r') 7 black = f.read() 8 f.close() 9 #三次登录信息 10 name1 = input('Please enter name:') 11 #检查name1是否在black_list 12 if name1 in black: 13 print ('Sorry, you cannt login') 14 #不在black_list 15 else: 16 for i in range(3): 17 password1 = input("Please enter password:") 18 if name == name1 and password == password1: 19 print ('Welcome!') 20 21 else: 22 print ('Your password is wrong') 23 count+=1 24 #登录三次锁定账号 25 if count == 3: 26 print ('Your account is locked.') 27 f = open('black_list.txt','a') 28 f.write(name1+' ') 29 f.close()