import os
def menuInfo():
print('==================学生管理系统====================')
print('--------------------功能菜单---------------------')
print(' 1、录入学生信息')
print(' 2、查询学生信息')
print(' 3、删除学生信息')
print(' 4、修改学生信息')
print(' 5、排序')
print(' 6、统计学生人数')
print(' 7、显示所有学生信息')
print(' 0、退出')
print('------------------------------------------------')
stu_file = 'student.txt'
def main():
while True:
menuInfo()
choice = int(input('请选择编号:'))
if choice == 0:
answer = input('您确定要退出吗?y/n:')
if answer == 'y' or answer == 'Y':
print('感谢您的使用!')
break # 退出系统
else:
continue
elif choice == 1:
insert()
elif choice == 2:
query()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
sort()
elif choice == 6:
calcTotalNum()
elif choice == 7:
show()
def insert():
student_list = []
while True:
id = input('请输入ID(如1001):')
if not id:
break
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as file:
student_old = file.readlines()
for item in student_old:
d = dict(eval(item))
if d['id'] == id:
print('该ID已存在!')
insert()
return
name = input('请输入姓名:')
if not name:
break
try:
english = int(input('请输入英语成绩:'))
python = int(input('请输入python成绩:'))
java = int(input('请输入Java成绩:'))
except:
print('输入无效,不是整数类型,请重新输入')
continue
# 将录入的信息存到字典中
student = {'id': id, 'name': name, 'english': english, 'python': python, 'java': java}
# 将学生信息添加到列表中
student_list.append(student)
answer = input('是否继续添加?y/n:')
if answer == 'y':
continue
else:
break
# 调用save()函数
save(student_list)
print('学生信息录入完毕!')
def save(lst):
try:
stu_text = open(stu_file, 'a', encoding='utf-8')
except:
stu_text = open(stu_file, 'w', encoding='utf-8')
for item in lst:
stu_text.write(str(item)+'\n')
stu_text.close()
def query():
student_query = []
stu_id = ''
stu_name = ''
while True:
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
answer = input('按ID查找请输入1,按姓名查找请输入2:')
if answer == '1':
stu_id = input('请输入学生ID:')
elif answer == '2':
stu_name = input('请输入学生姓名:')
else:
print('输入不规范,请重新输入')
query()
for item in students:
d = dict(eval(item))
if d['name'] == stu_name and stu_name:
student_query.append(d)
elif d['id'] == stu_id and stu_id:
student_query.append(d)
# 显示查询结果
show_student(student_query)
# 清除列表
student_query.clear()
answer = input('是否继续查询?y/n:')
if answer == 'y' or answer == 'Y':
continue
else:
break
else:
print('没有学生信息!!!')
return
def show_student(lst):
if len(lst) == 0:
print('未查到对应的学生信息')
else:
# 定义显示格式
format_style = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
print(format_style.format('ID', '姓名', 'English成绩', 'Python成绩', 'Java成绩', '总成绩'))
for item in lst:
print(format_style.format(item.get('id'),
item.get('name'),
item.get('english'),
item.get('python'),
item.get('java'),
int(item.get('english'))+int(item.get('python'))+int(item.get('java'))))
def delete():
while True:
student_id = input('请输入需要删除的学生ID:')
if student_id:
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as file:
student_old = file.readlines()
else:
student_old = []
flag = False
if student_old:
with open(stu_file, 'w', encoding='utf-8') as wfile:
d = {}
for item in student_old:
d = dict(eval(item))
if d['id'] != student_id:
wfile.write(str(d)+'\n')
else:
flag = True
if flag:
print(f'id为{student_id}的学生信息已经被删除了!')
else:
print(f'没有找到ID为{student_id}的学生')
else:
print('没有学生信息')
break
show()
answer = input('是否继续删除?y/n:')
if answer == 'y':
continue
else:
break
def modify():
while True:
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as file:
student_old = file.readlines()
else:
return
student_id = input('请输入需要修改的学生ID:')
if student_id:
if student_old:
with open(stu_file, 'w', encoding='utf-8') as wfile:
d = {}
flag = True
for item in student_old:
d = dict(eval(item))
if d['id'] == student_id:
flag = False
print('找到学生信息,可以进行修改')
while True:
try:
englishScore = d['english']
d['english'] = input(f'原来的English成绩为{englishScore},请输入新的english成绩:')
pythonScore = d['python']
d['python'] = input(f'原来的python成绩为{pythonScore},请输入新的python成绩:')
javaScore = d['java']
d['java'] = input(f'原来的java成绩为{javaScore},请输入新的java成绩:')
wfile.write(str(d) + '\n')
print('修改成功!')
except:
print('输入的格式有误,请重新输入')
else:
break
else:
wfile.write(str(d) + '\n')
if flag:
print(f'未找到ID为{student_id}的学生信息')
else:
print('没有学生信息可以被修改')
answer = input('是否继续修改其他同学信息?y/n:')
if answer == 'y':
continue
else:
break
def sort():
show()
stu_new = []
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
if students:
for item in students:
stu_new.append(dict(eval(item)))
else:
print('还没有录入学生信息')
else:
print('还没有录入学生信息')
asc_or_desc = input('请选择(0、升序 1、降序)')
asc_or_desc_bool = False
if asc_or_desc == '0':
asc_or_desc_bool = False
elif asc_or_desc == '1':
asc_or_desc_bool = True
else:
print('输入有误,请重新输入')
sort()
mode = input('请输入排序方式(1、按照英语成绩排序 2、按照python成绩排序 3、按照java成绩排序 0、按照总成绩排序)')
if mode == '1':
stu_new.sort(key=lambda x: int(x['english']), reverse=asc_or_desc_bool)
elif mode == '2':
stu_new.sort(key=lambda x: int(x['python']), reverse=asc_or_desc_bool)
elif mode == '3':
stu_new.sort(key=lambda x: int(x['java']), reverse=asc_or_desc_bool)
elif mode == '0':
stu_new.sort(key=lambda x: int(x['english'])+int(x['python'])+int(x['java']), reverse=asc_or_desc_bool)
else:
print('输入有误,请重新输入')
sort()
show_student(stu_new)
def calcTotalNum():
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
if students:
print(f'共有{len(students)}个学生')
else:
print('还没有录入学生信息')
else:
print('还没有录入学生信息')
def show():
if os.path.exists(stu_file):
stu_list = []
with open(stu_file, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
for item in students:
stu_list.append(dict(eval(item)))
show_student(stu_list)
else:
print('还没有录入学生信息')
if __name__ == '__main__':
main()
python实战1——学生管理系统
最新推荐文章于 2024-09-27 08:45:19 发布