1. 程式人生 > >用戶登錄程序,讀取文件用戶名,鎖定寫入文件

用戶登錄程序,讀取文件用戶名,鎖定寫入文件

bcd close 登錄程序 += 錯誤 登錄 寫入 lis pen

讀取文件:

mport json

user_list = {
‘andy‘: "123456",
‘william‘:‘123456‘,
‘abc‘: "123456",
‘bcd‘:‘123456‘,
‘cde‘: ‘123456‘,
‘def‘: "123456",
‘efg‘: ‘123456‘,
}

jsObj = json.dumps(user_list)
fileObject = open(‘jsonFile.json‘, ‘w‘)
fileObject.write(jsObj)
fileObject.close()

用戶登錄程序:
#Author:xiesongyou
import json
times = 0
login = False
while times < 3 and login == False: #用戶名錯可以輸入3次用戶名
with open(‘login_3_times.json‘) as json_file:
data = json.load(json_file)
username=input("請輸入用戶名>>")
if username in data:
print("用戶已鎖定,請聯系管理員!")
login = True
else:
fileObject = open(‘jsonFile.json‘)
user_list = json.load(fileObject)
if username in user_list:
count = 0
userpwd = input("請輸入密碼>>")
while count<3:

if userpwd == user_list[username]:
login = True
print("歡迎登陸...")
break
count += 1
if count < 3: #可以輸入3次密碼
userpwd = input("請重新輸入密碼>>")
else:
with open(‘login_3_times.json‘) as json_file:
data = json.load(json_file)
data[username]="123456"

jsObj = json.dumps(data)
fileObject = open(‘login_3_times.json‘,"w")
fileObject.write(jsObj)
fileObject.close()
print("用戶已鎖定!")
else:

times += 1
print("用戶名錯誤!%d 次"%times)









用戶登錄程序,讀取文件用戶名,鎖定寫入文件