用户登录程序,读取文件用户名,锁定写入文件

读取文件:

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)









posted @ 2018-01-21 10:11  沧海一粒水  阅读(152)  评论(0编辑  收藏  举报