pyhton学习,day1作业,用户名密码登录模块

要求,通过用户名密码登录,登录错误3次,锁定用户名

 1 # coding=utf-8
 2 # Author: RyAn Bi
 3 
 4 import os, sys  #调用系统自己的库
 5 
 6 accounts_file = 'E:\\homework\\user.txt'  # 存放用户名密码的位置
 7 lock_file = 'E:\\homework\\lock.txt'  #存放锁定用户名的位置
 8 print('accounts_file:', accounts_file) 
 9 '''
10 temp1 = open(accounts_file,'a') #a为加入
11 temp1.write('bjb,123'+'\n')
12 temp1.close()  #写入用户名密码的功能,在登录的时候不用
13 '''
14 user = open(accounts_file, 'r') #r为只读
15 # print('user:',user)
16 account_list = user.readlines()  #逐行按分隔符阅读,readline是逐字节阅读
17 # print('account_list:',account_list)
18 user.close()   #读取用户名和密码到内存中
19 
20 user1 = open(lock_file, "r")   
21 lock_list = user1.readlines()
22 # print(lock_list)
23 user1.close()  #读取被锁定的用户名到内存中
24 loginSucess = False   #默认将loginSucess置为假
25 while True:
26     username = input('username:').strip()  #.strip是去除空格和\n
27     lockid = 0        #锁定用户标识值为零,不锁定
28     if len(username) != 0:      #假如用户名不为空
29         for i in lock_list:
30             # i = i.split(' ')   #i的分割符为空格
31             # print(i)
32             if username == i.strip():
33                 print('your username %s is locked!' % username)
34                 lockid = 1
35                 break  # 判断该帐号是否被锁定
36         for i in account_list:
37             # print(i)
38             if lockid == 1:  #被锁定了禁止执行
39                 break
40             else:
41                 i = i.split(',')
42                 # print(i)
43                 if username == i[0]:
44                     for x in range(3):
45                         password = input('password:').strip()
46                         if password == i[1].strip():  # 去掉空格和换行符
47                             loginSucess = True
48                             # print('you are right')
49                             break   #验证3次密码,成功了置 loginsuccess 为真
50                     else:
51                         print('%s,you input wrong password 3 times,your %s is locked!' % (username, username))
52                         l = open(lock_file, 'w') #w只允许写入,以写入方式打开locklist
53                         if l != "":
54                             l = open(lock_file, 'a') #假如是locklist中有内容,用添加方式打开locklist,避免空白行
55                         l.write(username + '\n') #将用户名写入
56                         l.close()
57                         lockid = 1
58                         # view = open(lock_file)
59                         # print(view.read())
60         if loginSucess is True:
61             print('welcome to system')
62             break
63         elif lockid == 0:
64             print('you input a wrong username,pls input again')
65         else:
66             # print('%s,you input wrong password 3 times,your %s is locked!' % (username, username))
67             break
68     else:  # 用户名输入为空的时候
69         continue

猜你喜欢

转载自www.cnblogs.com/bbgoal/p/10237816.html