python写完成代码,实现学生成绩录入系统升级版
时间: 2024-10-21 21:05:59 浏览: 19
Python编写一个学生成绩录入系统升级版,我们可以设计一个简单的命令行交互界面,支持添加、查看、修改和删除学生信息。这里是一个基础的示例:
```python
class Student:
def __init__(self, id, name, scores):
self.id = id
self.name = name
self.scores = scores
class GradeSystem:
def __init__(self):
self.students = {}
def add_student(self, id, name, scores):
self.students[id] = Student(id, name, scores)
def view_students(self):
for id, student in self.students.items():
print(f"ID: {id}, Name: {student.name}, Scores: {student.scores}")
def update_score(self, id, new_score):
if id in self.students:
self.students[id].scores = new_score
print("Score updated successfully.")
else:
print("Student not found.")
def delete_student(self, id):
if id in self.students:
del self.students[id]
print("Student deleted.")
else:
print("Student not found.")
# 创建grade_system实例并使用它
grade_system = GradeSystem()
while True:
action = input("Enter action (add, view, update, delete, quit): ")
if action == "quit":
break
elif action == "add":
id = int(input("Enter student ID: "))
name = input("Enter student name: ")
scores = [float(score) for score in input("Enter scores separated by comma: ").split(",")]
grade_system.add_student(id, name, scores)
elif action == "view":
grade_system.view_students()
elif action == "update":
id = int(input("Enter student ID to update: "))
new_score = float(input("Enter new score: "))
grade_system.update_score(id, new_score)
elif action == "delete":
id = int(input("Enter student ID to delete: "))
grade_system.delete_student(id)
else:
print("Invalid action. Please try again.")
```
这个程序允许用户通过输入指令来操作学生信息,如添加新学生、查看所有学生、更新分数和删除学生。你可以根据需要增加更多功能,比如按成绩排序、查找特定学生等。
阅读全文