定义学生类,包括学号、姓名、性别、多门课程的成绩;假设有n个学生,由键盘输入学生信息; 定义类成员函数,计算每个学生的平均成绩; 按课程统计每门课程的平均成绩、最高分和最低分;(可选) 同时输出所有学生信息; 通过输入学生学号或姓名查询某学生的信息。
时间: 2023-11-16 11:31:08 浏览: 135
class Student:
def __init__(self, number, name, gender, scores):
self.number = number
self.name = name
self.gender = gender
self.scores = scores
def get_average_score(self):
return sum(self.scores) / len(self.scores)
@staticmethod
def get_course_info(students, course_index):
scores = [student.scores[course_index] for student in students]
average_score = sum(scores) / len(scores)
max_score = max(scores)
min_score = min(scores)
return average_score, max_score, min_score
@staticmethod
def print_all_students(students):
for student in students:
print(f"Number: {student.number}, Name: {student.name}, Gender: {student.gender}, Scores: {student.scores}")
@staticmethod
def search_student(students, query):
for student in students:
if student.number == query or student.name == query:
return student
return None
n = int(input("Enter the number of students: "))
students = []
for i in range(n):
number = input("Enter the student's number: ")
name = input("Enter the student's name: ")
gender = input("Enter the student's gender: ")
scores = []
for j in range(3):
score = float(input(f"Enter the score of course {j+1}: "))
scores.append(score)
student = Student(number, name, gender, scores)
students.append(student)
for i in range(3):
average_score, max_score, min_score = Student.get_course_info(students, i)
print(f"Course {i+1}: Average Score: {average_score}, Max Score: {max_score}, Min Score: {min_score}")
Student.print_all_students(students)
query = input("Enter the student's number or name to search: ")
student = Student.search_student(students, query)
if student:
print(f"Number: {student.number}, Name: {student.name}, Gender: {student.gender}, Scores: {student.scores}")
else:
print("Student not found.")
阅读全文