设计并测试一个名为mystudent的类。该类包括以下属性:id,name,以及三门课程math、English、computer的成绩,类中的方法包括三门课程的总分、平均分和最高分并显示
时间: 2024-06-10 14:05:34 浏览: 11
非常好,这是一个创建并测试一个类的问题。为了回答这个问题,我们需要编写一个Python类,命名为mystudent,并且包括id、name和三门课程math、English、computer的成绩。 我们需要在类中定义三个方法:计算三门课程的总分、平均分和最高分。
下面是解决该问题的Python代码:
class mystudent:
def __init__(self, id, name, math, english, computer):
self.id = id
self.name = name
self.math = math
self.english = english
self.computer = computer
def total_score(self):
return self.math + self.english + self.computer
def average_score(self):
return self.total_score() / 3
def highest_score(self):
return max(self.math, self.english, self.computer)
# 测试代码
s = mystudent(1001, 'John', 89, 78, 95)
print("ID: ", s.id)
print("Name: ", s.name)
print("Math: ", s.math)
print("English: ", s.english)
print("Computer: ", s.computer)
print("Total Score: ", s.total_score())
print("Average Score: ", s.average_score())
print("Highest Score: ", s.highest_score())
这段代码会输出以下内容:
ID: 1001
Name: John
Math: 89
English: 78
Computer: 95
Total Score: 262
Average Score: 87.33333333333333
Highest Score: 95
这个类成功地定义了学生信息,并且能够计算三门课程的总分、平均分和最高分,并将结果显示出来。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)