实现学生选修课程成绩管理系统,要求如下: 1. 编写类记录学生基本信息、学生选修的课程及学分; 2. 使用菜单提供以下功能,每个功能通过函数实现: • 添加新的学生; • 记录学生的选修课程及分数; • 使用文件存储学生的信息,并能够读取和显示; • 查询学生课程选修记录,显示平均成绩。
时间: 2024-04-07 19:30:13 浏览: 125
为了实现学生选修课程成绩管理系统,我们可以创建以下类来记录学生的基本信息和选修课程:
```python
class Student:
def __init__(self, name, age, student_id):
self.name = name
self.age = age
self.student_id = student_id
self.courses = []
def add_course(self, course, credits, score):
self.courses.append({"course": course, "credits": credits, "score": score})
def get_average_score(self):
total_score = 0
total_credits = 0
for course in self.courses:
total_score += course["score"]
total_credits += course["credits"]
if total_credits == 0:
return 0
return total_score / total_credits
```
接下来,我们可以创建一个管理系统类来处理学生的添加、记录成绩、存储和查询功能:
```python
class CourseManagementSystem:
def __init__(self):
self.students = []
def add_student(self, name, age, student_id):
student = Student(name, age, student_id)
self.students.append(student)
def add_course_score(self, student_id, course, credits, score):
for student in self.students:
if student.student_id == student_id:
student.add_course(course, credits, score)
break
def save_students(self, filename):
with open(filename, "w") as file:
for student in self.students:
file.write(f"{student.name},{student.age},{student.student_id}\n")
for course in student.courses:
file.write(
f"{course['course']},{course['credits']},{course['score']}\n"
)
def load_students(self, filename):
self.students = []
with open(filename, "r") as file:
lines = file.readlines()
for i in range(0, len(lines), 2):
student_info = lines[i].strip().split(",")
student = Student(student_info[0], int(student_info[1]), student_info[2])
course_info = lines[i + 1].strip().split(",")
for j in range(0, len(course_info), 3):
student.add_course(
course_info[j], int(course_info[j + 1]), float(course_info[j + 2])
)
self.students.append(student)
def get_average_score(self, student_id):
for student in self.students:
if student.student_id == student_id:
return student.get_average_score()
return 0
```
现在,我们可以使用以上功能创建一个菜单来操作学生选修课程成绩管理系统:
```python
def main():
cms = CourseManagementSystem()
while True:
print("1. 添加学生")
print("2. 记录学生选修课程及分数")
print("3. 存储学生信息")
print("4. 载入学生信息")
print("5. 查询学生平均成绩")
print("6. 退出")
choice = input("请选择操作:")
if choice == "1":
name = input("请输入学生姓名:")
age = int(input("请输入学生年龄:"))
student_id = input("请输入学生学号:")
cms.add_student(name, age, student_id)
print("学生添加成功!\n")
elif choice == "2":
student_id = input("请输入学生学号:")
course = input("请输入选修课程名称:")
credits = int(input("请输入课程学分:"))
score = float(input("请输入课程分数:"))
cms.add_course_score(student_id, course, credits, score)
print("课程分数记录成功!\n")
elif choice == "3":
filename = input("请输入文件名:")
cms.save_students(filename)
print("学生信息保存成功!\n")
elif choice == "4":
filename = input("请输入文件名:")
cms.load_students(filename)
print("学生信息载入成功!\n")
elif choice == "5":
student_id = input("请输入学生学号:")
average_score = cms.get_average_score(student_id)
print(f"学生平均成绩为:{average_score}\n")
elif choice == "6":
break
else:
print("无效的选择,请重新输入!\n")
if __name__ == "__main__":
main()
```
以上就是一个简单的学生选修课程成绩管理系统的实现。你可以根据自己的需求进行修改和扩展。
阅读全文