Python学生成绩管理系统
时间: 2023-11-29 15:45:02 浏览: 95
python学生成绩管理系统
以下是一个简单的Python学生成绩管理系统,包括录入学生信息、查找学生信息、删除学生信息、修改学生信息、排序、统计学生总人数和显示所有学生信息等功能:
```python
import os
filename = 'students.txt'
# 录入学生信息
def insert():
student_list = []
while True:
id = input('请输入学生ID(如1001):')
if not id:
break
name = input('请输入学生姓名:')
if not name:
break
try:
english = int(input('请输入英语成绩:'))
python = int(input('请输入Python成绩:'))
c = int(input('请输入C语言成绩:'))
except:
print('输入无效,不是整数类型,请重新录入。')
continue
# 将录入的学生信息保存到字典中
student = {'id': id, 'name': name, 'english': english, 'python': python, 'c': c}
# 将学生信息添加到列表中
student_list.append(student)
answer = input('是否继续添加?(y/n):')
if answer == 'n':
break
# 将学生信息保存到文件中
save(student_list)
# 查找学生信息
def search():
student_id = input('请输入要查找的学生ID:')
if student_id:
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
for student in students:
student = eval(student)
if student['id'] == student_id:
print(f"找到学生信息,学生ID:{student['id']},姓名:{student['name']},英语成绩:{student['english']},Python成绩:{student['python']},C语言成绩:{student['c']}")
break
else:
print('未找到该学生信息!')
else:
print('暂未保存学生信息!')
# 删除学生信息
def delete():
student_id = input('请输入要删除的学生ID:')
if student_id:
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
with open(filename, 'w', encoding='utf-8') as wfile:
for student in students:
student = eval(student)
if student['id'] != student_id:
wfile.write(str(student))
print(f'ID为{student_id}的学生信息已被删除!')
else:
print('暂未保存学生信息!')
# 修改学生信息
def modify():
student_id = input('请输入要修改的学生ID:')
if student_id:
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
with open(filename, 'w', encoding='utf-8') as wfile:
for student in students:
student = eval(student)
if student['id'] == student_id:
student['name'] = input('请输入姓名:')
student['english'] = int(input('请输入英语成绩:'))
student['python'] = int(input('请输入Python成绩:'))
student['c'] = int(input('请输入C语言成绩:'))
wfile.write(str(student))
print(f'ID为{student_id}的学生信息已被修改!')
else:
print('暂未保存学生信息!')
# 排序
def sort():
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
students = [eval(student) for student in students]
asc_or_desc = input('请选择(0升序;1降序):')
if asc_or_desc == '0':
students.sort(key=lambda x: x['id'], reverse=False)
elif asc_or_desc == '1':
students.sort(key=lambda x: x['id'], reverse=True)
else:
print('输入错误,请重新输入!')
sort()
show_student(students)
else:
print('暂未保存学生信息!')
# 统计学生总人数
def total():
if os.path.exists(filename):
with open(filename, '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(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
if students:
show_student(students)
else:
print('暂未录入学生信息!')
else:
print('暂未保存学生信息!')
# 将学生信息保存到文件中
def save(student):
try:
students_txt = open(filename, 'a', encoding='utf-8')
except Exception as e:
students_txt = open(filename, 'w', encoding='utf-8')
for info in student:
students_txt.write(str(info) + '\n')
students_txt.close()
# 显示学生信息
def show_student(students):
print('学生信息如下:')
print('学生ID\t\t姓名\t\t英语成绩\tPython成绩\tC语言成绩\t总成绩')
for student in students:
print(f"{student['id']}\t\t{student['name']}\t\t{student['english']}\t\t{student['python']}\t\t{student['c']}\t\t{student['english'] + student['python'] + student['c']}")
# 菜单显示函数
def menu():
print('============学生信息管理系统============')
print('---------------功能菜单---------------')
print('\t\t\t1.录入学生信息')
print('\t\t\t2.查找学生信息')
print('\t\t\t3.删除学生信息')
print('\t\t\t4.修改学生信息')
print('\t\t\t5.排序')
print('\t\t\t6.统计学生总人数')
print('\t\t\t7.显示所有学生信息')
print('\t\t\t0.退出系统')
print('-------------------------------------')
# 主函数
def main():
while True:
menu()
option = input('请选择操作:')
option_str = ['0', '1', '2', '3', '4', '5', '6', '7']
if option not in option_str:
print('输入错误,请重新输入!')
continue
elif option == '0':
answer = input('确定要退出系统吗?(y/n):')
if answer == 'y':
break
else:
continue
elif option == '1':
insert()
elif option == '2':
search()
elif option == '3':
delete()
elif option == '4':
modify()
elif option == '5':
sort()
elif option == '6':
total()
elif option == '7':
show()
if __name__ == '__main__':
main()
```
阅读全文