import json
class InputError(Exception):
def __str__(self):
return '请根据提示输入正确的数字'
class StuSys:
def __init__(self, sys_name):
self.sys_name = sys_name
def show_home(self):
return """
======================================
欢迎来到%s学生管理系统
◆ 1.登 录
◆ 2.注 册
◆ 3.退 出
======================================""" % self.sys_name
def start_sys(self):
flag = 1
while flag:
print(self.show_home())
choice1 = int(input('请选择(1-3):'))
if choice1 == 1:
StuSys.login_in()
elif choice1 == 2:
StuSys.register()
elif choice1 == 3:
break
else:
raise InputError
@classmethod
def login_in(cls):
"""登录"""
while True:
account = input('请输入账号:')
password = input('请输入密码:')
stu = {'account': account, 'password': password}
with open('students.txt', 'r', encoding='utf-8') as f:
students = json.load(f)
stu_accounts = []
for student in students:
if student['account'] == account:
if student['password'] ==password:
input('登陆成功')
return
else:
input('登录失败!密码错误!')
return
else:
input('登录失败!账号不存在!')
return
@classmethod
def register(cls):
"""注册"""
while True:
flag = 0
account = input('请输入账号:')
password = input('请输入密码:')
stu = {'account': account, 'password': password}
with open('students.txt', 'r', encoding='utf-8') as f:
students = json.load(f)
for student in students:
if student['account'] == account:
input('账号已存在,请按下任意键重新输入账号')
flag = 1
break
if flag:
continue
else:
students.append(stu)
with open('students.txt', 'w', encoding='utf-8') as f:
json.dump(students, f)
input('注册成功! 请按下任意键返回上一层')
break
def main():
c1 = StuSys('千峰')
c1.start_sys()
if __name__ == '__main__':
main()
网友评论