擅长:python、mysql、java
<p>为什么不把完整的成绩字典发送到你的班级,并将其存储在一个变量中。
(请注意,在Python中,行尾没有分号)</p>
<pre><code>class Student:
def __init__(self, firstName, lastName, id, grade_dict):
self._firstName = firstName
self._lastName = lastName
self._id = id
self._grades = grade_dict
def get_grades(self):
return self._grades
</code></pre>
<p>然后,当您要初始化和使用成绩时:</p>
<pre><code>student1 = Student("Edward", "Gates", "0456789", {'math': 100, 'bio': 90, 'history': 80})
grades = student1.get_grades()
for key, value in grades.items():
print 'Marks in {}: {}'.format(key, str(value))
</code></pre>
<p>打印内容:</p>
<pre><code>Marks in bio: 90
Marks in math: 100
Marks in history: 80
</code></pre>