需求:写一个注册的程序,账号和密码都存在文件里面。
#choice = input('请输入你的选择:1,注册2、删除用户3、登录')
#注册:输入账号、密码、密码确认
1.需要校验用户是否存在,两次输入的密码,是否一致,为空的情况
2.账号和密码都存在文件里面
#删除用户:输入一个用户名
1.需要校验用户是否存在
#登录:输入账号密码登录
user_info={}#dict, 存放users文件中的username-pwd
with open('users') as f:
for line in f:
line=line.strip().split(',')#去空格并逗号分隔
user_info[line[0]]=line[1]#user_info存放users文件中的username-pwd
for i in range(3):
choice = input("请输入你的选择:1.登录 2.注册 3.删除用户")
if choice=='1':
username=input('username:').strip()
pwd=input('pwd:').strip()
if not username and not pwd:
print('账号密码不能为空!')
else:
if username not in user_info:
print("账户不存在")
else:
if user_info.get(username)!=pwd:
print('账号密码错误')
else:
print('登录成功')
elif choice=='2':
username=input('username:').strip()
pwd=input('pwd:').strip()
cpwd=input('cpwd:').strip()
if not username and not pwd and not cpwd:
print("输入不能为空!")
else:
if username in user_info:
print('用户已经被注册!')
else:
if pwd != cpwd:
print("两次输入密码不一致")
else:
user_info[username]=pwd
print('恭喜注册成功!')
elif choice=='3':
username=input('username:').strip()
if not username:
print('输入不能为空')
else:
if username not in user_info:
print('账户不存在')
else:
user_info.pop(username)#删除k=username的元素
print('账号删除成功')
else:
print("输入不正确,请重新输入")
else:#for循环完后需要把字典user_info更新到users文件
with open('users','w') as fw:
for username,pwd in user_info.items():
fw.write(username+','+pwd+'\n')