import file_manager
import student_system
#=================注册
"""
[
{'user_name':'password'} 改良了格式
]
保存到userinfo.json中
"""
file_name = 'user_info.json'
def login():
user_name=input('请输入用户名:')
password=input('请输入密码:')
all_user = get_all_user()
for user in all_user:
for key in user:
if key == user_name:
if user[key] == password:
print('登录成功')
return user_name
else:
print('密码错误,登录失败')
return None
print('没有注册,登录失败!')
return None
def get_all_user():
all_user = file_manager.read_json_file(file_name)
if all_user == None:
return []
return all_user
def register():
while True:
username = input('请输入一个用户名(3-10位):')
if not 3<=len(username)<=10:
print('输入有误,请重新输入')
continue
all_user = file_manager.read_json_file(file_name)
if not all_user:
all_user = []
for user in all_user:
for key in user:
if key == username:
print('%s,已经注册过,请重新输入'%username)
return register() #运用回调函数重新返回输入用户名
print('用户名可用')
break
while True:
password = input('请输入密码(6-16位):')
if not 6<=len(password)<=16:
print('密码输入有误,请重新输入')
continue
re_password = input('确认密码:')
if password != re_password:
print('和第一次输入的密码不一样,请重新输入')
continue
break
all_user = get_all_user()
all_user.append({username:password})
re = file_manager.write_json_file(file_name,all_user)
if re:
print('注册成功')
else:
print('注册失败')
#==================主页
def show_main_page():
while True:
print(file_manager.read_file('login.txt'))
value = input('请选择(1-3):')
if value == '3':
break
elif value == '2':
register()
elif value == '1':
user_name = login()
if user_name:
student_system.user_name = user_name
student_system.main_page()
else:
print('输入有误!')
if __name__ == '__main__':
show_main_page()
网友评论