# -*- coding:utf-8 -*-#Author:Mr Ganimport syslimit_time = 3loggin_time = 0while loggin_time < limit_time: user = input('\033[31;1m username:\033[0m') ...
# -*- coding:utf-8 -*-
#Author:Mr Gan
import sys
limit_time = 3
loggin_time = 0
while loggin_time < limit_time:
user = input('\033[31;1m username:\033[0m').strip()
with open('account_lock','r') as lock_name:
for lock_line in lock_name:
if user == lock_line.strip():
print('you have been locked')
if len(user) == 0:#判斷是否輸入空用戶名
print('username is not none')
continue
password = input('\033[31;1m password: \033[0m').strip()
with open('account', 'r') as account_name:
mathch_flag = False #設置登錄認證標誌位
for user_line in account_name.readlines():
username, passwd = user_line.strip().split()
if (user,password) == (username,passwd):
print('%s login success'% user)
mathch_flag = True#設置登錄認證標誌位
break #跳出for迴圈
if mathch_flag == False and loggin_time < 2: #登錄認證不匹配
print('username or password error,please try again')
loggin_time += 1
else: #登錄認證成功
with open('account_lock', 'a+') as f:
f.write('%s\n'% user )
print('you have been locked ')
break
代碼不足之處:用戶被鎖定時,只是第三次輸入的用戶名被鎖定到文件中,不符合邏輯