python3封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,语文成绩;求总分,平均分,以及打印输出学生的相关信息。
时间: 2024-01-04 16:04:16 浏览: 86
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
可以这样实现:
```python
class Student:
def __init__(self, name, age, gender, english_score, math_score, chinese_score):
self.name = name
self.age = age
self.gender = gender
self.english_score = english_score
self.math_score = math_score
self.chinese_score = chinese_score
def total_score(self):
return self.english_score + self.math_score + self.chinese_score
def average_score(self):
return self.total_score() / 3
def print_info(self):
print("Name:", self.name)
print("Age:", self.age)
print("Gender:", self.gender)
print("English score:", self.english_score)
print("Math score:", self.math_score)
print("Chinese score:", self.chinese_score)
print("Total score:", self.total_score())
print("Average score:", self.average_score())
```
然后可以创建一个学生对象并调用相关方法:
```python
student = Student("Tom", 18, "Male", 80, 90, 85)
student.print_info()
```
输出:
```
Name: Tom
Age: 18
Gender: Male
English score: 80
Math score: 90
Chinese score: 85
Total score: 255
Average score: 85.0
```
阅读全文