怎样把这一串代码改成class形式
ef student_score(name, chinese, math, english, science):
return {"name": name, "chinese": chinese, "math": math, "english": english, "science": science}
def score_sum(student):
return student["chinese"] + student["math"] + student["english"] + student["science"]
def score_avg(student):
return score_sum(student)/4
def score_print(student):
return print(student["name"], score_sum(student), score_avg(student), sep="\t")
# 女学生名单.
students_women = [
student_score("PING", 64, 88, 92, 92 )
student_score("BASMA", 64, 88, 92, 92 ),
student_score("MEIJUN", 64, 88, 92, 92 ),
student_score("WENQI", 64, 88, 92, 92 ),
student_score("ZHILING", 64, 88, 92, 92 ),
student_score("JINHUA", 64, 88, 92, 92 ),
student_score("DO", 64, 88, 92, 92 )
]
# 男学生名单.
students_men = [
student_score("HAIMING", 64, 88, 92, 92 ),
student_score("ZHENG", 64, 88, 92, 92 ),
student_score("KONG", 64, 88, 92, 92 )
]
# 重复女学生名单.
print("女学生分数")
print("名字", "总分", "平均分", sep="\t")
for student in students_women:
score_print(student)
# 重复男学生名单.
print("\n男学生分手")
print("名字", "总分", "平均分", sep="\t")
for student in students_men:
score_print(student)
class Student():
def __init__(self, name, chinese, math, english, science):
self.name = name
self.chinese = chinese
self.math = math
self.english = english
self.science = science
def score_sum(self):
return self.chinese + self.math + self.english + self.science
def score_avg(self):
return self.score_sum() / 4
def score_print(self, ):
return print(self.name, self.score_sum(), self.score_avg(), sep="\t")
# 女学生名单.
students_women = [
Student("PING", 64, 88, 92, 92 ),
Student("BASMA", 64, 88, 92, 92 ),
Student("MEIJUN", 64, 88, 92, 92 ),
Student("WENQI", 64, 88, 92, 92 ),
Student("ZHILING", 64, 88, 92, 92 ),
Student("JINHUA", 64, 88, 92, 92 ),
Student("DO", 64, 88, 92, 92 )
]
# 男学生名单.
students_men = [
Student("HAIMING", 64, 88, 92, 92 ),
Student("ZHENG", 64, 88, 92, 92 ),
Student("KONG", 64, 88, 92, 92 )
]
# 重复女学生名单.
print("女学生分数")
print("名字", "总分", "平均分", sep="\t")
for student in students_women:
student.score_print()
# 重复男学生名单.
print("\n男学生分数")
print("名字", "总分", "平均分", sep="\t")
for student in students_men:
student.score_print()