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

Posted 沧海一粒水

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用户登录程序,读取文件用户名,锁定写入文件相关的知识,希望对你有一定的参考价值。

读取文件:

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)









以上是关于用户登录程序,读取文件用户名,锁定写入文件的主要内容,如果未能解决你的问题,请参考以下文章

作业一:登录界面(优化,能读取锁定文件中的任意用户名,一旦发现所输入的用户名是锁定文件中的,立即告知并跳出循环)

Python 模拟三次用户登录,登录失败锁定账号并写入文件

Python脚本实现基于文件存储的用户登录程序

用户登录程序

如何写入/读取“设置”文本文件

day1作业--登录接口