[Python] 纯文本查看 复制代码 ##用户平台信息登陆系统
import os
import json
from time import sleep
def creat_user():
f = open(r"用户信息表.txt","r+")
js = f.read()
js = js.replace("\n","")
js = js.replace("\'","\"")
js = js.replace("}{",",")#全部都是为了json格式转换成字典,我也是醉了……
comfirm = json.loads(js)
comfirm = comfirm.keys()
i = 2
j = 2#跳出验证循环的阻断变量
while j > 1:
new_name = str(input("请输入你想要注册的账号:"))
if new_name in comfirm:#判断是否存在此账号了!
print("账号已存在!!请重新输入!!\a\n")
elif "" ==new_name or " " in new_name:
print("请输入合法字符,不允许有空白字符和不输入字符!")
else:
j = 1#如果成功则跳出循环
#用户账号验证过程
while i > 1:
new_password = str(input("请输入你的密码:"))
comfirm_password = str(input("请再次输入你的密码:"))
if "" == new_password or " " in new_password:
print("请输入合法字符,不允许有空白字符和不输入字符!")
continue
else:
if new_password == comfirm_password:
r = "注册成功!!请保管好您的账号和密码!"
i = 1
else:
print("两次密码不一致,请重新输入你的密码:\n")
#用户密码验证过程
new_user = dict()
new_user[new_name] = new_password
f.write(str(new_user))#如果用json方法也是可以的
f.close()#关闭文件惹
return r
def login():
i = 2#跳出登陆循环的阻断变量
j = ""#判断用户是否创建新账号的变量
g = 2#创建新账号的循环阻断变量
h = 2
f = open(r"用户信息表.txt","r")
js = f.read()
js = js.replace("\n","")
js = js.replace("\'","\"")
js = js.replace("}{",",")#全部都是为了json格式转换成字典,我也是醉了……
comfirm = json.loads(js)
comfirm_values = comfirm.values()
comfirm_keys = comfirm.keys()
f.close()#用完即关闭文件
while True:
login_user = str(input("请输入登录账号:"))
if "" ==login_user or " " in login_user:
print("请输入正确用户账号!\n")
continue
elif login_user not in comfirm_keys:
while True:
j = str(input("账号不存在!!是否要新建账号(y/n)\a:"))
if len(j) == 0 or " " in j:
print("输入有误,请重新输入!!\n")
elif j == "Y"or j == "y":
if creat_user() == "注册成功!!请保管好您的账号和密码!":
break
elif j == "N" or j == "n":
break
else:
print("不能输入Y\\N(不区分大小写)以外的字符!!\n")
print("正在跳转到登录页面……\n")
sleep(1)
login()
break
while h > 1:
login_password = str(input("请输入登录密码:"))
if login_password == comfirm[login_user]:
print("登录成功,欢迎回来!\n\a")
break#跳出密码输入错误,重新输入的循环
else:
print("密码错误!请重新输入\n")
break
def menus():
print("\t菜单选择:\n")
print("0.注册新账号")
print("1.登陆账号")
print("2.退出系统")
while True:
menu = str(input())
if "" == menu or " " in menu or menu.isalpha():
print("输入有误!\n")
continue
elif int(menu) == 0:
creat_user()
menus()
elif int(menu) == 1:
login()
menus()
elif int(menu) == 2:
exit()
menus()
用户信息表.txt
(42 Bytes, 下载次数: 19)
那个txt文件一定要放在源代码相同文件夹下。如果没有,新建了新文件夹会导致读取数据异常而中断程序 |