封装一个学生类: (1)有姓名,有年龄,有性别,有英语成绩,数学成绩,语文成绩; (2)求总分,平均分; (3)打印输出学生的相关信息。python代码
时间: 2023-11-13 19:05:57 浏览: 12
学生成绩录入
```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())
# 示例用法
student1 = Student("Tom", 18, "Male", 80, 90, 95)
student1.print_info()
```
阅读全文