定义一个学生类,属性包含姓名,学号,语数外三门的成绩
要求:能够设置学生某科目的成绩;能打印该学生的所有科目成绩
点击查看代码
class Student:
def __init__(self,student_name,student_id):
self.name = student_name
self.student_id = student_id
self.grades = {"语文":0,"数学":0,"英语":0}
def change_grade(self,course,grade):
if course in self.grades: #条件判断别忘记
self.grades[course] = grade
def print_grades(self):
print(f"学生{self.name}(学号:{self.student_id})的成绩为:")
for course in self.grades: #循环输出
print(f"{course}:{self.grades[course]}分")
chen = Student("小陈","10086")
zeng = Student("小曾","100330")
print(chen.name)
print(zeng.grades)
chen.change_grade("语文",92)
chen.change_grade("数学",88)
chen.print_grades()