1. 定义学生类,包括学号、姓名、性别、多门课程的成绩;假设有n个学生,由键盘输入学生信息; 2.定义类成员函数,计算每个学生的平均成绩; 3.按课程统计每门课程的平均成绩、最高分和最低分; 4.同时输出所有学生信息; 5. 通过输入学生学号或姓名查询某学生的信息。
时间: 2023-12-15 15:09:19 浏览: 29
class Student:
def __init__(self, number, name, gender, scores):
self.number = number
self.name = name
self.gender = gender
self.scores = scores
def average_score(self):
return sum(self.scores) / len(self.scores)
@staticmethod
def course_statistics(students):
courses = [[] for _ in range(len(students[0].scores))]
for student in students:
for i in range(len(student.scores)):
courses[i].append(student.scores[i])
course_avg = [sum(course) / len(course) for course in courses]
course_max = [max(course) for course in courses]
course_min = [min(course) for course in courses]
return course_avg, course_max, course_min
@staticmethod
def print_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, keyword):
for student in students:
if keyword in student.number or keyword in student.name:
return student
return None
if __name__ == '__main__':
n = int(input("Enter the number of students: "))
students = []
for i in range(n):
number = input(f"Enter the number of student {i + 1}: ")
name = input(f"Enter the name of student {i + 1}: ")
gender = input(f"Enter the gender of student {i + 1}: ")
scores = [int(score) for score in input(f"Enter the scores of student {i + 1} separated by space: ").split()]
student = Student(number, name, gender, scores)
students.append(student)
for student in students:
print(f"Average score of {student.name}: {student.average_score()}")
course_avg, course_max, course_min = Student.course_statistics(students)
for i in range(len(course_avg)):
print(f"Course {i + 1}: average score {course_avg[i]}, max score {course_max[i]}, min score {course_min[i]}")
Student.print_students(students)
keyword = input("Enter the student number or name to search: ")
searched_student = Student.search_student(students, keyword)
if searched_student:
print(f"Number: {searched_student.number}, Name: {searched_student.name}, Gender: {searched_student.gender}, Scores: {searched_student.scores}")
else:
print("Student not found.")
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)