在代码执行之前, 要先创建两个 txt 文件分别存储用户信息, 第一个存用户的账号, 密码, 年龄, 性别, 第二个文件只存账号, 密码.
- user = []
- password = []
- sex = []
- age = []
- file = open(r'.vscode\userinfo.txt','a+')
- file.seek(0)
- for line in file:
- user.append(line.split(',')[0])
- password.append(line.split(',')[1])
- sex.append(line.split(',')[2])
- age.append(line.split(',')[3].strip('\n'))
- print(user)
- print(password)
- print(sex)
- print(age)
- print('''欢迎进入注册用户程序
- 1 打印用户列表
- 2 注册用户
- 3 用户登陆
- 4 查找用户信息
- ''')
- while 1:
- temp = input("输入你的选择:")
- item = int(temp)
- if item == 2:
- name = input("请输入你的用户名:")
- password1 = input("请输入你的密码:")
- password2 = input("请再次输入你的密码:")
- if name ==''or password1 =='' or password2 == '':
- print("你的用户名与密码不能为空!")
- continue
- elif name in user:
- print('你输入的用户名已存在!')
- continue
- else:
- if password1 == password2:
- print("恭喜你,%s 注册成功" %name)
- sex1 = input("请输入你的性别")
- age1 = input("请输入你的年龄")
- file.write(name+','+password1+','+sex1+','+age1+'\n')
- print("用户存储成功")
- file.close()
- break
- else:
- print("密码不一致!")
- if item ==1 :
- print("name".center(8),"password".center(8),"sex".center(7),"age".center(6))
- for x,y,j,k in zip(user,password,sex,age):
- print(x.center(8),y.center(8),j.center(5),k.center(7))
- if item == 3:
- name = input("请输入你的用户名")
- password3 = input("请输入密码:")
- for i in range(len(user)):
- if name == user[i] :
- if password3 == password[i]:
- with open(r'.vscode\saveuser.txt','a') as f:
- f.write(user[i]+','+password[i]+'\n')
- print("登陆成功!")
- break
- else:
- print("密码输入错误!")
- if item ==4:
- name = input("请输入你的用户名")
- for i in range(len(user)):
- if name == user[i]:
- print("name".center(8),"sex".center(7),"age".center(6))
- print(user[i].center(8),sex[i].center(6),age[i].center(6))
来源: http://www.bubuko.com/infodetail-2863959.html