学生管理系统
import json
def home_page(): # 首页界面
print('='*40)
print('**欢迎来到学生管理系统**'.center(30, ' '))
print('◆□1.登 录'.center(36, ' '))
print('◆□2.注 册'.center(36, ' '))
print('◆□3.退 出'.center(36, ' '))
print('=' * 40)
n = int(input('请选择(1-3):'))
if n == 1:
log_in()
elif n == 2:
sign_in()
elif n == 3:
pass
else:
print('输入数据无效')
home_page()
def log_in(): # 登录界面
print('=' * 40)
print('**登录**'.center(38, ' '))
global user_name
user_name = input('请输入用户名:')
pw = input('请输入密码(3-6位):')
with open('data.json', 'r', encoding='utf-8') as f:
content = json.loads(f.read())
for dict1 in content:
if dict1['username'] == user_name:
if dict1['password'] == pw:
print('登录成功!')
return menu()
else:
print('登录失败!密码输入错误')
return home_page()
else:
print('登录失败!用户没注册!')
return home_page()
def sign_in(): # 注册界面
print('=' * 40)
print('**注册**'.center(38, ' '))
user_name1 = input('请输入用户名:')
while True:
pw = input('请输入密码(3-6位):')
pw1 = input('请确认密码:')
if len(pw) < 3 or len(pw) > 6:
print('输入密码不规范')
continue
elif pw != pw1:
print('两次输入的密码不一致')
continue
else:
break
with open('data.json', 'r', encoding='utf-8') as f:
content = json.loads(f.read())
for dict1 in content:
if dict1['username'] == user_name1:
print('该用户名已注册')
return home_page()
with open('data.json', 'w', encoding='utf-8') as f:
content.append({'username': user_name1, 'password': pw, 'students': []})
f.write(json.dumps(content))
print('注册成功!')
return home_page()
def menu(): # 菜单界面
print('=' * 40)
print('☺☺欢迎%s' % user_name)
print('☀ 1. 添加学生')
print('☀ 2. 查看学生')
print('☀ 3. 修改学生信息')
print('☀ 4. 删除学生')
print('☀ 5. 返回')
n = int(input('请选择:'))
if n == 1:
return add_student()
def add_student(): # 添加学生
global student
print('=' * 40)
print('**添加学生信息**'.center(36, ' '))
with open('data.json', 'r', encoding='utf-8') as f:
content = json.loads(f.read())
# print(content)
for dict1 in content:
if dict1['username'] == user_name:
student = dict1
break
name = input('输入学生姓名:')
# print(student)
for index in student['students']:
if name == index['name']:
print('该学生已存在!')
return menu()
age = int(input('输入学生年龄:'))
gender = input('输入学生性别:')
tel = input('输入电话号码:')
student['students'].append({'name': name, 'age': age, 'gender': gender, 'tel': tel})
with open('data.json', 'w', encoding='utf-8') as f:
f.write(json.dumps(content))
print('添加成功!')
print('='*40)
print('1.继续添加')
print('2.返回')
n = int(input('请选择:'))
if n == 1:
return add_student()
elif n == 2:
return menu()
if __name__ == '__main__':
home_page()