我尝试创建一个学生类对象,并收集一些数据并显示它:
class Student:
subjects = []
grades = []
d = {}
def __init__(self, name, id):
self.name = name
self.id = id
def addGrade(self, subject, grade):
self.subjects.append(subject)
self.grades.append(grade)
def showGrades(self):
self.d = dict(zip(self.subjects, self.grades))
for subject in self.d:
return subject + ' : ' + str(self.d[subject])
当我尝试添加值时:
stu =Student('Zaki', 23)
stu.addGrade('Math',90)
stu.addGrade('Physicis',95)
stu.addGrade('English',100)
print(stu.showGrades())
输出结果为 Math : 90
并且它忽略了其他值!
牧羊人nacy
aluckdog
相关分类