Python通过面向对象的方式根据成绩输出学生相关信息案例

题目描述

通过面向对象的方式,建立学生成绩类,类中属性包含学生姓名、学号、数学成绩、英语成绩、编程课成绩,录入5名学生信息,编程输出,这5个成绩类中单科最高成绩的学生信息,及总成绩最高的学生信息。

编写思路

首先定义了一个Student类,包含了学生的姓名、学号、数学成绩、英语成绩和编程课成绩等属性。在类中,我们还定义了两个方法:get_highest_score_subject用于获取单科最高成绩的科目,get_total_score用于计算学生的总成绩。

在主程序中,我们通过循环输入5名学生的信息,并创建Student对象,将它们存储在列表students中。

接下来,我们遍历students列表,使用get_highest_score_subject方法找到单科最高成绩的学生,并将其存储在highest_score_subject_students列表中。

然后,我们输出单科最高成绩的学生信息。

最后,我们通过计算每个学生的总成绩,找到总成绩最高的学生,并输出其信息。

完整代码

以下是使用面向对象方式实现的Python代码,创建了一个名为Student的学生成绩类,并实现了录入学生信息、计算最高成绩和输出学生信息的功能:

class Student:
    def __init__(self, name, student_id, math_score, english_score, programming_score):
        self.name = name
        self.student_id = student_id
        self.math_score = math_score
        self.english_score = english_score
        self.programming_score = programming_score

    def get_highest_score_subject(self):
        subjects = {'Math': self.math_score, 'English': self.english_score, 'Programming': self.programming_score}
        highest_score_subject = max(subjects, key=subjects.get)
        return highest_score_subject

    def get_total_score(self):
        return self.math_score + self.english_score + self.programming_score


# 录入5名学生信息
students = []
for i in range(5):
    name = input("请输入学生姓名:")
    student_id = input("请输入学生学号:")
    math_score = float(input("请输入学生数学成绩:"))
    english_score = float(input("请输入学生英语成绩:"))
    programming_score = float(input("请输入学生编程课成绩:"))

    student = Student(name, student_id, math_score, english_score, programming_score)
    students.append(student)

# 查找单科最高成绩的学生信息
highest_score_subject_students = []
for student in students:
    highest_score_subject = student.get_highest_score_subject()
    if student.math_score == highest_score_subject or \
            student.english_score == highest_score_subject or \
            student.programming_score == highest_score_subject:
        highest_score_subject_students.append(student)

# 输出单科最高成绩的学生信息
print("单科最高成绩的学生信息:")
for student in highest_score_subject_students:
    print("姓名:", student.name)
    print("学号:", student.student_id)
    print("数学成绩:", student.math_score)
    print("英语成绩:", student.english_score)
    print("编程课成绩:", student.programming_score)
    print()

# 查找总成绩最高的学生信息
total_scores = [student.get_total_score() for student in students]
highest_total_score_index = total_scores.index(max(total_scores))
highest_total_score_student = students[highest_total_score_index]

# 输出总成绩最高的学生信息
print("总成绩最高的学生信息:")
print("姓名:", highest_total_score_student.name)
print("学号:", highest_total_score_student.student_id)
print("数学成绩:", highest_total_score_student.math_score)
print("英语成绩:", highest_total_score_student.english_score)
print("编程课成绩:", highest_total_score_student.programming_score)

 

© 版权声明
THE END
喜欢就支持一下吧
点赞10赞赏 分享