python使用类名实现学生成绩管理系统
时间: 2023-12-25 12:30:17 浏览: 72
以下是使用类名实现学生成绩管理系统的示例代码:
```python
class Student:
def __init__(self, student_id, name, score):
self.student_id = student_id
self.name = name
self.score = score
class StudentManagerSystem:
student_list = []
student_dict = {}
def add_student(self, student_id, name, score):
student = Student(student_id, name, score)
self.student_list.append(student)
self.student_dict[student_id] = student
def delete_student(self, student_id):
if student_id in self.student_dict:
student = self.student_dict.pop(student_id)
self.student_list.remove(student)
def update_student(self, student_id, name, score):
if student_id in self.student_dict:
student = self.student_dict[student_id]
student.name = name
student.score = score
def search_student(self, student_id):
if student_id in self.student_dict:
student = self.student_dict[student_id]
return f"学号:{student.student_id},姓名:{student.name},成绩:{student.score}"
else:
return "学生不存在"
def display_menu(self):
print("1. 添加学生")
print("2. 删除学生")
print("3. 修改学生信息")
print("4. 查询学生信息")
print("5. 退出系统")
def start(self):
while True:
self.display_menu()
choice = input("请输入操作编号:")
if choice == "1":
student_id = input("请输入学号:")
name = input("请输入姓名:")
score = input("请输入成绩:")
self.add_student(student_id, name, score)
print("添加成功")
elif choice == "2":
student_id = input("请输入要删除的学号:")
self.delete_student(student_id)
print("删除成功")
elif choice == "3":
student_id = input("请输入要修改的学号:")
name = input("请输入新的姓名:")
score = input("请输入新的成绩:")
self.update_student(student_id, name, score)
print("修改成功")
elif choice == "4":
student_id = input("请输入要查询的学号:")
result = self.search_student(student_id)
print(result)
elif choice == "5":
print("退出系统")
break
else:
print("输入有误,请重新输入")
# 使用示例
system = StudentManagerSystem()
system.start()
```
阅读全文