流程图:
![]()
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #Author:SKING
4 """
5 题目:
6 输入账号,密码
7 1.输入三次后提示休息5s
8 2.密码重复输入三次后锁定账户
9 说明:
10 locked_user.txt 存储锁定的账号 初始内容为空
11 user_password.txt 存储用户名和密码 用户名和密码中间用'\t'制表符间隔
12 """
13
14 import string, sys, time
15
16 def wait_5s(): #等待5秒的函数
17 print('You input too many times,please wait 5s...')
18 for a in range(5, 1, -1):
19 print(f'{a}s')
20 time.sleep(1)
21
22 count = 0 #记录用户输入的次数
23 count_pwd = 3 #记录用户输入密码的次数
24
25 while count<3: #超过三次调用休息5s函数
26 count +=1
27 username = input('username:') #输入用户名
28 if username == '':
29 print('用户名不能为空!请重新输入!')
30 if count == 3:
31 wait_5s() #超过三次调用休息5s函数
32 count = 0
33 continue
34 #打开'locked_user.txt'文件
35 with open('locked_user.txt', 'r', encoding='utf-8') as file_locked_user:
36 for i in file_locked_user: #循环读取file_locked_user
37 i = i.strip() #去除i中的空格 换行等
38 if username == i:
39 print(f'{username} is locked!')
40 chose_key = input('Press "Q" to exit!Press any key to continue:')
41 if chose_key == 'Q' or chose_key == 'q':
42 sys.exit(0)
43 # 打开'user_password.txt'文件
44 with open('user_password.txt', 'r', encoding='utf-8') as file_user_password:
45 for j in file_user_password:
46 (file_username, file_password) = j.strip().split('\t') #提取用户名、密码
47 if username == file_username:
48 while count_pwd > 0:
49 print(f'You have {count_pwd} times,then will locked!')
50 password = input('password:')
51 if password == file_password:
52 print('Welcome to Python!') #账号、密码正确登录系统
53 sys.exit(0)
54 else:
55 print('Password is wrong,Please re-enter...')
56 count_pwd -= 1
57 if count_pwd == 0: #输错三次密码,账号就没锁定
58 #打开'locked_user.txt'文件并写入要锁定的账号
59 with open('locked_user.txt', 'r+', encoding='utf-8') as write_locked_user:
60 write_locked_user.write(username)
61 print(f'{username} is locked!')
62 sys.exit(0)
63 else:
64 print(f'{username} is not exist,Please re-enter...')
65
66 if count == 3: #如果输入超过三次就等待5s
67 wait_5s()
68 count = 0