2、写一个注册的程序,账号和密码都存在文件里面。
choice = input('请输入你的选择:1,注册2、删除用户3、登录')
注册
输入
账号
密码
密码确认
#需要校验用户是否存在,两次输入的密码,是否一致,为空的情况
账号和密码都存在文件里面
删除
输入一个用户名
需要校验用户是否存在
登录
输入账号密码登录

user_info = {} #存放所有的用户
with open('users.txt') as f:
for line in f:
# niuhanyang,123456\n
line = line.strip()
temp = line.split(',')
username = temp[0]
pwd = temp[1]
user_info[username]=pwd
for i in range(3):
choice = input('请输入你的选择'
'1、登录 2、注册 3、删除').strip()
if choice=='1':
username = input('username:').strip()
pwd = input('pwd:').strip()
if username and pwd:
if username in user_info:
if user_info.get(username)==pwd:
print('登录成功')
else:
print('账号密码错误!')
else:
print("user not found!")
else:
print('账号密码不能为空!')
elif choice=='2':
username = input('username:').strip()
pwd = input('pwd:').strip()
cpwd = input('cpwd:').strip()
if username and pwd and cpwd:
if username in user_info:
print('该用户已经被注册!')
else:
if pwd==cpwd:
user_info[username]=pwd
print('恭喜,注册成功!')
else:
print('两次输入的密码不一致!')
else:
print('不能为空!')
elif choice=='3':
username = input('username:').strip()
if username:
if username in user_info:
user_info.pop(username)
print('删除成功!')
else:
print('不能为空!')
else:
print("输入有误,请重新输入")
else:
with open('users.txt','w') as fw:
for uname,pwd in user_info.items():
fw.write(uname+','+pwd+'\n')
posted on 2018-04-22 20:30  彼得潘jd  阅读(1285)  评论(0编辑  收藏  举报