#改造登录注册的程序 #账号密码存到文件里面 #要判断用户是否存在

#定义一个登录函数:
def login(name,pwd):
username1=input("请输入用户名:")
password1=input("请输入密码:")
for i in range(3):
if username1 == name and password1 == pwd:
print("登陆成功")
break
elif username1=='' or password1=='':
print("用户名和密码不能为空")
else:
print('用户名和密码错误,请重新输入!')
else:
print("用户名和密码输入错误超过三次!程序结束!")
#定义一个注册函数
def regit():
username1 = input("请输入用户名:")
password1 = input("请输入密码:")
password2 = input("请输入确认密码:")
if password1 == password2 and username1 != '':
print("注册成功")
dict.setdefault(username1, password1)
else:
print('请按照规范填写用户名和密码!')
return dict

#读取文件中的用户名和密码
dict={}
f=open('a.txt','r',encoding='utf-8')
result=f.read()
res=result[0:-1]
new_res=res.split("\n") #new_res是一个list
f.close()
for i in new_res:
nn_res=i.split(',')
dict.setdefault(nn_res[0],nn_res[1])
username=input("请输入用户名:")
if username in dict:
print("已经注册,请输入密码登录程序")
login(username,dict.get(username))
else:
print("请注册!")
regit()
f=open('a.txt','w',encoding='utf-8')
for key,value in dict.items():
f.write('%s,%s' % (key, value))
f.write('\n') # 换行写入

猜你喜欢

转载自www.cnblogs.com/wanglun1101/p/12387906.html