# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: 学生管理系统
Description :
Author : LPP
date: 2018/7/27
-------------------------------------------------
Change Activity:
2018/7/27:
-------------------------------------------------
"""
__author__ = 'LPP'
import json
def check_name(path):
try:
with open('./'+ path + '.json', 'r') as f:
return False
except FileNotFoundError:
return True
def login(user_name, password):
try:
with open('./'+ user_name + '.json', 'r') as f:
info = json.load(f)
if password == info['password']:
print('登录成功!!!')
return True
else:
print('密码错误!!!')
return False
except FileNotFoundError:
print('登录失败,用户名不存在')
def register(user_name, password):
try:
with open('./' + user_name + '.json', 'r') as f:
return False
except FileNotFoundError:
with open('./' + user_name + '.json', 'w') as f:
json.dump({"user_name": user_name, "password": password, "count": 0, "student": []}, f)
return True
def main():
print('================================\n\
1. 录入学生信息\n\
2. 查看学生信息\n\
3. 查看所有学生信息\n\
4. 删除学生信息\n\
5. 退出登录\n\
================================\n')
def add_student(stu_info):
with open('./' + user_name + '.json', 'r', encoding='utf-8') as f:
all_info = json.load(f)
all_info['count'] += 1
stu_id = 'py1805' + str(all_info['count']).rjust(3, '0')
stu_info.update({"学号":stu_id})
all_info['student'].append(stu_info)
with open('./' + user_name + '.json', 'w', encoding='utf-8') as f:
json.dump(all_info, f)
def find_student(ways, flag):
if flag == '1':
names = []
with open('./' + user_name + '.json', 'r', encoding='utf-8') as f:
info = json.load(f)
students = info['student']
count = 0
for stu in students:
if stu['姓名'] == ways:
names.append(stu)
count +=1
if count == 0:
print('查无此人!!!')
return False
return names
elif flag == '2':
with open('./' + user_name + '.json', 'r', encoding='utf-8') as f:
info = json.load(f)
students = info['student']
count = 0
for stu in students:
if stu['学号'] == ways:
count += 1
return stu
if count == 0:
print('查无此人!!!')
return False
else:
print('错误的查询方式')
return False
def delete_student(id):
with open('./' + user_name + '.json', 'r', encoding='utf-8') as f:
info = json.load(f)
students = info['student']
for stu in students:
if id == stu['学号']:
students.remove(stu)
with open('./' + user_name + '.json', 'w', encoding='utf-8') as f:
json.dump(info, f)
if __name__ == '__main__':
while True:
print(' ================================')
print('| 1. 登录 |')
print('| 2. 注册 |')
print('| 3. 退出 |')
print(' ================================')
ipt = input("请输入对应数字执行操作:\n>")
if ipt == '1':
user_name = input('请输入用户名:\n>>')
password = input('请输入密码:\n>>')
if login(user_name, password):
while True:
main()
operation = input('请输入数字执行对应操作:\n>')
while operation == '1':
stu_info = {'姓名': None, '性别': None, '学号': None, '年龄': None, '成绩': {}}
stu_name = input('请输入学生姓名:\n>>')
stu_gender = input('请输入学生性别:\n>>')
stu_age = int(input('请输入学生年龄:\n>>'))
stu_info.update({'姓名': stu_name, '性别': stu_gender, '学号': None, '年龄': stu_age, '成绩': {}})
English_grade = int(input('请输入%s的英语成绩:\n>>' % stu_info['姓名']))
stu_info['成绩'].update({'English': English_grade})
PE_grade = int(input('请输入%s的体育成绩:\n>>' % stu_info['姓名']))
stu_info['成绩'].update({'PE': PE_grade})
Art_grade = int(input('请输入%s的美术成绩:\n>>' % stu_info['姓名']))
stu_info['成绩'].update({'Art': Art_grade})
Math_grade = int(input('请输入%s的数学成绩:\n>>' % stu_info['姓名']))
stu_info['成绩'].update({'Math': Math_grade})
add_student(stu_info)
print('添加成功!!!')
print('================================')
print('继续添加请输入1,否则请输入0')
op2 = input('请输入数字执行对应操作:\n>')
if op2 != '1':
break
while operation == '2':
print(' 1.按姓名查找 ')
print(' 2.按学号查找 ')
print(' 3.返回上一层 ')
op3 = input('请输入序号选择对应操作')
if op3 == '1':
find_ways = input('请输入需要查找的学生姓名')
result = find_student(find_ways, op3)
if result:
for x in range(len(result)):
print(x, result[x])
print('==================================')
op4 = input('是否继续操作? 是(Y),否(回车)')
if op4 == 'Y' or op4 == 'y':
continue
else:
break
elif op3 == '2':
find_ways = input('请输入需要查找的学生学号')
result = find_student(find_ways, op3)
print(result)
print('==================================')
op5 = input('是否继续操作? 是(Y),否(回车)')
if op5 == 'Y' or op5 == 'y':
continue
else:
break
else:
break
if operation == '3':
with open('./' + user_name + '.json', 'r', encoding='utf-8') as f:
info = json.load(f)
students = info['student']
if len(students):
for x in range(len(students)):
print(x, students[x])
print('================================')
op10 = input('回车返回上一层')
else:
print('系统为空')
print('================================')
op10 = input('回车返回上一层')
while operation == '4':
print(' 1.按姓名删除 ')
print(' 2.按学号删除 ')
print(' 3.返回上一层 ')
print('================================')
op6 = input('请输入序号选择对应操作')
if op6 == '1':
find_ways = input('请输入需要删除的学生姓名')
result = find_student(find_ways, op6)
if result:
for x in range(len(result)):
print(x, result[x])
op9 = input('请输入需要删除的学生对应的序号')
if op9 in str(range(len(result))):
delete_student(result[int(op9)]['学号'])
else:
print('输入有误')
print('==================================')
op7 = input('是否继续操作? 是(Y),否(回车)')
if op7 == 'Y' or op7 == 'y':
continue
else:
break
elif op6 == '2':
find_ways = input('请输入需要删除的学生学号')
result = find_student(find_ways, op6)
if result:
delete_student(find_ways)
else:
print('找不到此学号')
print('==================================')
op8 = input('是否继续操作? 是(Y),否(回车)')
if op8 == 'Y' or op8 == 'y':
continue
else:
break
else:
break
if operation == '5':
break
else:
continue
while ipt == '2':
register_name = input('请输入用户名:\n>>')
if check_name(register_name):
register_password = input('请输入密码:\n>>')
register_password2 = input('请再次输入密码:\n>>')
else:
print('用户名不可用,请重新输入!!!')
continue
if register_password == register_password2:
flag = register(register_name, register_password)
else:
print('两次输入密码不相同,请重新输入')
continue
if flag:
break
else:
print('注册失败')
op1 = input('是否继续注册?是(y),否(任意键回车)')
if op1 == 'y' or op1 == 'Y':
continue
break
if ipt == '3':
print('感谢使用,再见!!')
break
网友评论