python模拟用户登录系统,如何两个用户输入各自的密码才能登入?
users = {'root': ['123', False], 'westos': ['456', False]}
while True:
if all([x[1] for x in users.values()]):
print('two users login successfully')
break
user = input('input user name:\t')
if not users.get(user):
print('unexist user')
continue
else:
for i in range(3):
pw = input('input password:\t')
if users[user][0] == pw:
print(f'user `{user}` login successfully')
users[user][1] = True
break
用一个字典存储username,pw以及登录状态. 10行判断是否两个人登录状态都为True,如果是,则打印并退出while. 否则13行输入username,如果name不存在,while continue; 如果存在, 进入else,输入密码,密码对则打印并修改状态.超过3次退出for进入while.
import json #用来存储数据的模块
import os #用来进行文件操作
import sys #获取脚本所在目录用
import re #用来进行字符串操作
script_path = os.path.realpath(__file__)
PATH = os.path.dirname(script_path)
contents_list = []
if not os.path.exists(PATH+'\\'+'user_information'):
os.mkdir(PATH+'\\'+'user_information')
#主体函数,是主要的内容
def main(user_name):
obj = input('你现在想要干什么?\n1.新建一个文件 2.查看一个文件 3.更改密码 4.更改用户名\n')
if obj == '1':
file_name = input('请输入文件名(不用带后缀名):')
print('请输入文件内容(如果完成输入的话就新建一行,然后输入“输入已完成”并回车来完成输入,如:\n这是第一行\n这是第二行\n这是第\
三行\n这是文件的最后一行\n输入已完成\n\n请输入:')
while True:
contents = input()
if contents == '输入已完成':
break
else:
contents_list.append(contents)
with open(PATH+'\\'+user_name+'.mmwj','r') as f:
file_names = f.readlines()
if file_name+'\n' in file_names:
print('害!写了半天,你文件名还是重复了,害!')
input('按回车键退出……')
else:
with open(PATH+'\\'+user_name+'\\'+file_name+'.json','w') as f:
json.dump(contents_list,f)
with open(PATH+'\\'+user_name+'.mmwj','a') as f:
f.write(file_name+'\n')
print('已成功!')
input('按回车键退出……')
elif obj == '2':
with open(PATH+'\\'+user_name+'.mmwj','r') as f:
file_name_list = f.readlines()
temporary_file_name_list = []
for i in file_name_list:
i = re.sub('\"|\'','',i)
i = re.sub('\n','',i)
temporary_file_name_list.append(i)
file_name_list = temporary_file_name_list
if not file_name_list == []:
print('这是你存过的所有的文件的列表:')
for i in file_name_list:
print(i)
user_file_name = input('请输入你想要查看的文件的文件名(不用带后缀):')
if user_file_name in file_name_list:
with open(PATH+'\\'+user_name+'\\'+user_file_name+'.json','r') as f:
cont = json.load(f)
for i in cont:
print(i)
input('按回车键退出……')
else:
print('未找到此文件!')
input('按回车键退出……')
else:
print('空空如也……')
input('按回车键退出……')
elif obj == '3':
with open(PATH+'\\'+'user_information\\user_name_list.json','r') as n:
user_name_list_change = n.readlines()
tm_name_change = []
for i in user_name_list_change:
i = re.sub('\"\'','',i)
i = re.sub('\n','',i)
tm_name_change.append(i)
user_name_list_change = tm_name_change
with open(PATH+'\\'+'user_information\\user_password_list.json','r') as p:
user_password_list_change = p.readlines()
tm_password_change = []
for i in user_password_list_change:
i = re.sub('\"\'','',i)
i = re.sub('\n','',i)
tm_password_change.append(i)
user_password_list_change = tm_password_change
xyz = user_name_list_change.index('"'+user_name+'"')
old_right_password = user_password_list_change[xyz]
old_password = input('请输入旧密码:')
if '"' + old_password + '"' == old_right_password:
new_password = input('请输入新密码(不要带引号或中文!):')
user_password_list_change[xyz] = new_password
with open(PATH+'\\'+'user_information\\user_password_list.json','w') as np:
for i in user_password_list_change:
np.write('"'+i+'"'+'\n')
print('密码已更改!')
input('按回车键退出……')
else:
print('对不起,旧密码错误。')
input('按回车键退出……')
elif obj == '4':
with open(PATH+'\\'+'user_information\\user_name_list.json','r') as n:
user_name_list_change = n.readlines()
tm_name_change = []
for i in user_name_list_change:
i = re.sub('\"\'','',i)
i = re.sub('\n','',i)
tm_name_change.append(i)
user_name_list_change = tm_name_change
xyz = user_name_list_change.index('"'+user_name+'"')
new_name = input('请输入新用户名:')
if '"' + new_name + '"' in user_name_list_change:
print('用户名已存在。')
input('按回车键退出……')
else:
user_name_list_change[xyz] = '"' + new_name + '"'
os.rename(PATH+'\\'+user_name+'.mmwj',PATH+'\\'+new_name+'.mmwj')
os.rename(PATH+'\\'+user_name,PATH+'\\'+new_name)
with open(PATH+'\\'+'user_information\\user_name_list.json','w') as np:
for i in user_name_list_change:
np.write(i+'\n')
print('用户名已更改!')
input('按回车键退出……')
#注册函数
def sign_up():
user_name = input('请输入用户名(不要带引号或中文!):')
user_password = input('请输入密码(不要带引号或中文!):')
if user_name == 'user_information':
print('用户名无效!')
input('按回车键退出……')
if not os.path.exists(PATH+'\\'+'user_information\\user_name_list.json'):
with open(PATH+'\\'+'user_information\\user_name_list.json','w') as con:
json.dump(user_name,con)
con.write('\n')
with open(PATH+'\\'+'user_information\\user_password_list.json','w') as con:
json.dump(user_password,con)
con.write('\n')
os.mkdir(PATH+'\\'+user_name)
with open(PATH+'\\'+user_name+'.mmwj','w') as f:
pass
print('账户创建成功 !')
main(user_name)
else:
with open(PATH+'\\'+'user_information\\user_name_list.json','r') as f:
unsul = f.readlines()
if '"' + user_name + '"\n' in unsul:
print('用户名已存在。')
input('按回车键退出……')
else:
with open(PATH+'\\'+'user_information\\user_name_list.json','a') as con:
json.dump(user_name,con)
con.write('\n')
with open(PATH+'\\'+'user_information\\user_password_list.json','a') as con:
json.dump(user_password,con)
con.write('\n')
os.mkdir(PATH+'\\'+user_name)
with open(PATH+'\\'+user_name+'.mmwj','w') as f:
pass
print('账户创建成功 !')
main(user_name)
#内容函数
def content():
user_obj = input('请问你是想登录,还是想注册?(输入1或2):')
if user_obj == '1':
if not os.path.exists(PATH+'\\'+'user_information\\user_name_list.json'):
print('对不起,你现在还没有任何账户。')
yes_or_no = input('请问你要注册一个账户吗?(输入要或不要):')
if yes_or_no == '要':
sign_up()
if yes_or_no == '不要':
input('按回车键退出……')
else:
with open(PATH+'\\'+'user_information\\user_name_list.json','r') as n:
name_list = n.readlines()
temporary_name_list = []
for i in name_list:
i = re.sub('\"|\'','',i)
i = re.sub('\n','',i)
temporary_name_list.append(i)
name_list = temporary_name_list
with open(PATH+'\\'+'user_information\\user_password_list.json','r') as p:
password_list = p.readlines()
temporary_password_list = []
for i in password_list:
i = re.sub('\"|\'','',i)
i = re.sub('\n','',i)
temporary_password_list.append(i)
password_list = temporary_password_list
contrast = dict(zip(name_list,password_list))
user_name = input('请输入用户名:')
if user_name not in name_list:
print('未找到此用户名!')
input('按回车键退出……')
else:
password = input('请输入密码:')
xpassword = contrast[user_name]
if xpassword == password:
print('登录成功!')
main(user_name)
else:
print('密码输入错误。')
input('按回车键退出……')
elif user_obj == '2':
sign_up()
content()