#改造登录注册的程序 #账号密码存到文件里面 #要判断用户是否存在#a.txt文本中:username,password格式一行一条

#定义一个登录函数:
import datetime
def login(name,pwd):
password1=input("请输入密码:")
today=datetime.datetime.today()
for i in range(3):
if password1 == pwd:
print("欢迎%s登录,现在的时间是:%s。"%(name,today))
break
elif 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:
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') # 换行写入
posted @ 2020-03-01 03:56  半节黄土写代码  阅读(330)  评论(0编辑  收藏  举报