作业需求:
1、写一个登录程序,用用户密码认证
2、用户名密码存放在文件里
作业代码
1 #Author:abu zhang
2 #_*_coding:utf-8_*_
3 #提示输入用户名和密码
4 #验证输入用户名和密码
5 #如果认证错误,则输出用户名或密码错误
6 #如果成果,则输出,欢迎,XXX登陆
7 import sys
8 count = 0
9 while count<3:
10 count += 1
11 user_file = open('user_pass.txt','r') #打开用户名密码文件
12 user_list = user_file.readlines() #文件以列表形式存起来
13 username = input('Please enter your username:').strip() #输入用户名密码
14 if len(username) == 0:
15 print('用户名或密码不能为空,请重新输入。')
16 lock_file = open('lock_file.txt','r')
17 lock_list = lock_file.readlines()
18 for lock_line in lock_file:
19 lock_line = lock_line.strip('\n')
20 print(username)
21
22 if username == user_lock:
23 print('因为你输入次数较多,该用户已被锁住,请联系管理员')
24
25 for user_line in user_list: #循环用户名密码文件列表
26 user,passwd = user_line.strip('\n').split() #Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。 split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串
27 if username == user:
28 j = 0
29 while j < 3:
30 password = input('Please enter your password:').strip()
31 if len(password) == 0:
32 print('用户名或密码不能为空,请重新输入。')
33 if password == passwd:
34 exit('Welcome to %s system'% (username))
35 else:
36 if j != 2:
37 print('用户%s,密码错误,请重新输入,还有%d次机会'%(username,2-j))
38 j += 1
39 else:
40 lock_file = open('lock_file.txt','a')
41 lock_file.write(username + '\n')
42 lock_file.close()
43 sys.exit('用户%s达到最大登陆次数,将被锁定退出'%username)