案例补充:员工注册登录系统实现
while True:
print("""
1. 注册
2. 登录
""")
cmd = input('请输入你的选择:').strip()
# cmd的验证
if cmd == '1':
while True:
# 1. 接收用户名和密码
username = input('username>>>:')
password = input('password>>>:')
# 3. 判断用户是否已经注册过
# 3.1 先取出文件中得数据
with open('userinfo.txt', 'r', encoding='utf-8') as f1:
# 3.2 把文件数据一行一行的读出来
for line in f1:
# print(line) # kevin|123\n
real_username, *_ = line.split('|')
# res = line.split('|')
if real_username == username:
print('该用户已经存在,请从新输入')
break
else:
# 2. 组织用户名和密码成固定格式 kevin|123
data = '%s|%s\n' % (username, password)
# data = username + '|' + password
# 3. 把用户数据保存到文件中
with open('userinfo.txt', 'a', encoding='utf-8') as f:
f.write(data)
print('%s: 注册成功' % username)
elif cmd =='2':
username = input('username>>>:')
password = input('password>>>:')
# 2. 读取文件数据,得到用户的真实用户名和密码
with open('userinfo.txt', 'r', encoding='utf-8') as f:
# 一行一行的读取用户名和密码
for line in f:
real_username, real_pwd = line.split('|') # kevin|123\n ['kevin', '123\n']
real = real_pwd.strip('\n')
# 判断用户名和密码是否正确
if real_username == username and real == password:
print('登录成功')
break
else:
print('登录失败')
标签:username,real,登录,案例,注册,print,input,line,password
From: https://www.cnblogs.com/dream-ze/p/17455399.html