#!/usr/bin/env python3
# Author:Robert
# --*-- coding: utf-8 --*--
count
=
0
#記錄試錯(cuò)次數(shù),計(jì)數(shù)項(xiàng)賦初始值
load
=
True
#完成功能后退出,賦初始值
file
=
open
(
"用戶信息文件.txt"
,
'r'
,encoding
=
'utf-8'
)
file_lock
=
open
(
"鎖定用戶信息文件.txt"
,
'r+'
,encoding
=
'utf-8'
)
line
=
eval
(
file
.readline())
#將正確信息中的字符串轉(zhuǎn)換成字典(原字符串為字典格式)
line_list
=
eval
(file_lock.readline())
#將正確信息中的字符串轉(zhuǎn)換成列表(原字符串為列表格式)
def
out():
#定義重復(fù)代碼,目的是幫助跳出while循環(huán)并關(guān)閉已打開的文檔
global
load
load
=
False
#賦值load,為了跳出while循環(huán)
file_lock.close()
#關(guān)閉正確用戶信息文檔
file
.close()
#關(guān)閉鎖定用戶信息文檔
while
load:
#判斷是否已完成功能
name
=
input
(
"請(qǐng)輸入用戶名:"
)
password
=
input
(
"請(qǐng)輸入密碼:"
)
if
name
in
line
and
name
not
in
line_list:
#判斷用戶名是否正確,是否已被鎖定
while
count <
3
:
if
password
=
=
line[name]:
#判斷用戶名是否對(duì)應(yīng)正確的密碼
print
(
"您已成功登錄"
)
out()
#調(diào)用定義out方法
break
else
:
#說明未輸入的正確的密碼
count
+
=
1
msg_count
=
'第%s次密碼輸入錯(cuò)誤\n'
%
(count)
#提示輸入錯(cuò)誤次數(shù)
print
(msg_count)
if
count <
3
:
#小于三次錯(cuò)誤的輸入,可以重新輸入
password
=
input
(
"密碼錯(cuò)誤,請(qǐng)重新輸入密碼:"
)
#重新輸入密碼
elif
count
=
=
3
:
#判斷是否已輸錯(cuò)三次
print
(
"已輸錯(cuò)3次,賬號(hào)已鎖定"
)
line_list.append(name)
#將已鎖定信息加入鎖定元組中
file_lock.seek(
0
)
#輸入指針移到開頭,如果不移動(dòng)會(huì)產(chǎn)生多個(gè)元組
file_lock.write(
str
(line_list))
#寫入鎖定信息
file_lock.tell()
#獲取當(dāng)前的輸入指針位置,如果不獲取會(huì)產(chǎn)生多個(gè)元組
out()
break
elif
name
in
line_list:
#判斷用戶名是否在已鎖定用戶名中
print
(
"該用戶名已被鎖定"
)
out()
break
else
:
#說明用戶名不在正確用戶名信息中
print
(
"該用戶名不存在"
)
out()