用Python代码设计一套学生成绩管理系 统(学生人数至少为10人),要求满足以下要求: 程序运行后,需要显示系统主菜单: --主菜单------- 1、 录入学生信息 查找学生信息 2- 3、 删除学生信息 4、 修改学生信息 5、 学生成绩统计 6 退出系统 按照主菜单内容,对学生成绩进行相关操作并给出对应显示, 这里给出示例:请选择功能:1请输入学生学号:请输入学生姓名:请输入学生高数成绩: 请输入学生英语成绩: 请输入学生Python 成绩: 请输入学生C语言成绩:请输入学生机器学习成绩: 录入完毕?[YN:(输入Y,则询问下一个选择的功能;输入 N,则开始输入下一个学生的信息和成绩。) 在功能2下,则可以根据学生姓名或者是学生学号查找学生信息, 并将其所有成绩进行输出显示。 在功能5下,需要进行: a)计算每个学生所有课程的总成绩和平均分, b)每一门课程中所有学生的平均分 c) 将所有汇总信息保存到一个.csv文件中
时间: 2023-06-27 18:02:23 浏览: 400
【Python】学生成绩管理系统
以下是基于Python的学生成绩管理系统的代码实现:
```python
import csv
class Student:
def __init__(self, id, name, math_score, eng_score, py_score, c_score, ml_score):
self.id = id
self.name = name
self.math_score = math_score
self.eng_score = eng_score
self.py_score = py_score
self.c_score = c_score
self.ml_score = ml_score
def get_total_score(self):
return self.math_score + self.eng_score + self.py_score + self.c_score + self.ml_score
def get_avg_score(self):
return self.get_total_score() / 5
class Scoresheet:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def search_student_by_id(self, id):
for student in self.students:
if student.id == id:
return student
return None
def search_student_by_name(self, name):
for student in self.students:
if student.name == name:
return student
return None
def remove_student_by_id(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
return True
return False
def modify_student_by_id(self, id, name, math_score, eng_score, py_score, c_score, ml_score):
for student in self.students:
if student.id == id:
student.name = name
student.math_score = math_score
student.eng_score = eng_score
student.py_score = py_score
student.c_score = c_score
student.ml_score = ml_score
return True
return False
def get_total_avg_score(self):
total_score = 0
for student in self.students:
total_score += student.get_total_score()
return total_score / len(self.students)
def get_math_avg_score(self):
math_score = 0
for student in self.students:
math_score += student.math_score
return math_score / len(self.students)
def get_eng_avg_score(self):
eng_score = 0
for student in self.students:
eng_score += student.eng_score
return eng_score / len(self.students)
def get_py_avg_score(self):
py_score = 0
for student in self.students:
py_score += student.py_score
return py_score / len(self.students)
def get_c_avg_score(self):
c_score = 0
for student in self.students:
c_score += student.c_score
return c_score / len(self.students)
def get_ml_avg_score(self):
ml_score = 0
for student in self.students:
ml_score += student.ml_score
return ml_score / len(self.students)
def export_to_csv(self, filename):
with open(filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['学号', '姓名', '高数成绩', '英语成绩', 'Python成绩', 'C语言成绩', '机器学习成绩'])
for student in self.students:
writer.writerow([student.id, student.name, student.math_score, student.eng_score, student.py_score, student.c_score, student.ml_score])
def get_input(prompt, input_type=None):
while True:
try:
if input_type:
value = input_type(input(prompt))
else:
value = input(prompt)
return value
except:
print('输入的数据类型不正确,请重新输入!')
scoresheet = Scoresheet()
while True:
print('---主菜单---')
print('1、录入学生信息')
print('2、查找学生信息')
print('3、删除学生信息')
print('4、修改学生信息')
print('5、学生成绩统计')
print('6、退出系统')
choice = get_input('请选择功能:', int)
if choice == 1:
while True:
id = get_input('请输入学生学号:')
name = get_input('请输入学生姓名:')
math_score = get_input('请输入学生高数成绩:', float)
eng_score = get_input('请输入学生英语成绩:', float)
py_score = get_input('请输入学生Python成绩:', float)
c_score = get_input('请输入学生C语言成绩:', float)
ml_score = get_input('请输入学生机器学习成绩:', float)
student = Student(id, name, math_score, eng_score, py_score, c_score, ml_score)
scoresheet.add_student(student)
is_finished = input('录入完毕?[Y/N]:')
if is_finished.lower() == 'y':
break
elif choice == 2:
while True:
search_type = get_input('请选择查找类型(1-学号,2-姓名):', int)
if search_type == 1:
id = get_input('请输入要查找的学生学号:')
student = scoresheet.search_student_by_id(id)
if student:
print(f'学号:{student.id},姓名:{student.name},高数成绩:{student.math_score},英语成绩:{student.eng_score},Python成绩:{student.py_score},C语言成绩:{student.c_score},机器学习成绩:{student.ml_score}')
else:
print('未找到该学生信息!')
elif search_type == 2:
name = get_input('请输入要查找的学生姓名:')
student = scoresheet.search_student_by_name(name)
if student:
print(f'学号:{student.id},姓名:{student.name},高数成绩:{student.math_score},英语成绩:{student.eng_score},Python成绩:{student.py_score},C语言成绩:{student.c_score},机器学习成绩:{student.ml_score}')
else:
print('未找到该学生信息!')
else:
print('输入的查找类型不正确,请重新输入!')
is_continue = input('是否继续查找?[Y/N]:')
if is_continue.lower() != 'y':
break
elif choice == 3:
while True:
id = get_input('请输入要删除的学生学号:')
if scoresheet.remove_student_by_id(id):
print('删除成功!')
else:
print('未找到该学生信息!')
is_continue = input('是否继续删除?[Y/N]:')
if is_continue.lower() != 'y':
break
elif choice == 4:
while True:
id = get_input('请输入要修改的学生学号:')
name = get_input('请输入学生姓名:')
math_score = get_input('请输入学生高数成绩:', float)
eng_score = get_input('请输入学生英语成绩:', float)
py_score = get_input('请输入学生Python成绩:', float)
c_score = get_input('请输入学生C语言成绩:', float)
ml_score = get_input('请输入学生机器学习成绩:', float)
if scoresheet.modify_student_by_id(id, name, math_score, eng_score, py_score, c_score, ml_score):
print('修改成功!')
else:
print('未找到该学生信息!')
is_continue = input('是否继续修改?[Y/N]:')
if is_continue.lower() != 'y':
break
elif choice == 5:
total_avg_score = scoresheet.get_total_avg_score()
math_avg_score = scoresheet.get_math_avg_score()
eng_avg_score = scoresheet.get_eng_avg_score()
py_avg_score = scoresheet.get_py_avg_score()
c_avg_score = scoresheet.get_c_avg_score()
ml_avg_score = scoresheet.get_ml_avg_score()
print(f'所有学生的总平均分:{total_avg_score:.2f}')
print(f'高数的平均分:{math_avg_score:.2f}')
print(f'英语的平均分:{eng_avg_score:.2f}')
print(f'Python的平均分:{py_avg_score:.2f}')
print(f'C语言的平均分:{c_avg_score:.2f}')
print(f'机器学习的平均分:{ml_avg_score:.2f}')
filename = get_input('请输入导出的文件名:')
scoresheet.export_to_csv(filename)
print(f'成绩已导出到{filename}文件中!')
elif choice == 6:
print('谢谢使用!')
break
else:
print('输入的菜单编号不正确,请重新输入!')
```
阅读全文