我试图让用户继续输入模块和成绩并将其存储为字典{module:grades}并将此字典存储在列表中。
我有3个问题。
{完成:100} {完成:80}
依此类推。所以模块名称一直被“完成”这个词取代。
成绩:计算机科学:100 计算机图形学:80
我完成了余下的工作,而不是我坚持的这部分。非常感谢任何帮助。非常感谢。
students = []
class Student:
grades = {}
def setGrades(self, grades):
self.grades = grades
def addStudent():
while F6 != 'done':
F6 = raw_input("Please enter module name. type 'done' to quit: ")
if F6 == 'done':
break
F7 = raw_input("Please enter the grades for " ,F6, ':')
student.setGrades({F6:F7})
答案 0 :(得分:0)
对于初学者来说,你的代码(按原样)看起来应该是这样的
students = []
class Student:
grades = {}
def setGrades(self, grades):
self.grades = grades
def addStudent():
while True:
F6 = raw_input("Please enter module name. type 'done' to quit: ")
if F6 == 'done':
break
F7 = raw_input("Please enter the grades for " ,F6, ':')
student.setGrades({F6:F7})
在我看来,你也应该有更好的api来设置成绩。像这样的东西就足够了:
def set_grades(self, lesson, grade):
self.grades[lesson] = grade
最后,为了打印成绩,你应该有这样的方法:
def print_grades(self):
for lesson, grade in grades.items():
print lesson, grade
最后但并非最不重要的是,在您的原始输入中,为了使用F6,您必须执行以下操作:
F7 = raw_input("Please enter the grades for %s: " % F6)
总而言之,如果我是你,我的代码看起来像这样:
class Student:
grades = {}
def set_grades(self, lesson, grade):
self.grades[lesson] = grade
def addStudent():
while True:
F6 = raw_input("Please enter module name. type 'done' to quit: ")
if F6 == 'done':
break
F7 = raw_input("Please enter the grades for %s: " % F6)
student.setGrades(F6, F7)
def print_grades(self):
for lesson, grade in grades.items():
print lesson, grade