python学生成绩管理系统,一个简单的示例:
class Student:
def __init__(self, name, id):
self.name = name
self.id = id
self.scores = []
def add_score(self, score):
self.scores.append(score)
def average_score(self):
return sum(self.scores) / len(self.scores)
class StudentManager:
def __init__(self):
self.students = {}
def add_student(self, name, id):
self.students[id] = Student(name, id)
def add_score(self, id, score):
if id in self.students:
self.students[id].add_score(score)
else:
print("Student with ID {} not found.".format(id))
def get_average_score(self, id):
if id in self.students:
return self.students[id].average_score()
else:
print("Student with ID {} not found.".format(id))
def print_all_students(self):
for id, student in self.students.items():
print("ID: {}, Name: {}, Average Score: {}".format(id, student.name, student.average_score()))
# Example usage:
manager = StudentManager()
# Add students
manager.add_student("Alice", "1001")
manager.add_student("Bob", "1002")
manager.add_student("Charlie", "1003")
# Add scores
manager.add_score("1001", 90)
manager.add_score("1002", 80)
manager.add_score("1001", 95)
manager.add_score("1003", 85)
# Print average scores
print("Alice's average score:", manager.get_average_score("1001"))
print("Bob's average score:", manager.get_average_score("1002"))
print("Charlie's average score:", manager.get_average_score("1003"))
# Print all students
manager.print_all_students()
它的输出结果如下
C:\Users\fligh\PycharmProjects\classstudent\venv\Scripts\python.exe C:\Users\fligh\PycharmProjects\classstudent\main.py
Alice's average score: 92.5
Bob's average score: 80.0
Charlie's average score: 85.0
ID: 1001, Name: Alice, Average Score: 92.5
ID: 1002, Name: Bob, Average Score: 80.0
ID: 1003, Name: Charlie, Average Score: 85.0