为什么get_class_average(class_list)返回“字符串索引必须是整数”,所有以前的函数似乎都能正常工作?它应该使用以前重用的函数计算整个类的平均值。
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
def average(lst):
average = float(sum(lst)) / len(lst)
return average
def get_average(student):
score = average(student['homework']) * 0.1 + average(student['quizzes']) * 0.3 + average(student['tests']) * 0.6
return score
def get_letter_grade(score):
if score >= 90:
return "A"
elif 80 <= score < 90:
return "B"
elif 70 <= score < 80:
return "C"
elif 60 <= score < 70:
return "D"
elif score < 60:
return "F"
def get_class_average(class_list):
'''
get_class_average(['lloyd', 'alice', 'tyler'])
'''
total_class = 0
for student in class_list:
get_average(student)
total_class = total_class + get_average(student)
average_class = total_class / len(class_list)
return average_class
答案 0 :(得分:2)
你需要这样称呼它:
get_class_average([lloyd, alice, tyler])
您实际调用它的方式get_class_average(['lloyd', 'alice', 'tyler'])
,它会尝试访问'lloyd'['homework']
,这是没有意义的; 'lloyd'
是一个字符串,而不是dict
。
如果您希望能够传递名称,则必须使用另一个词典:
students = {'lloyd': lloyd, 'alice' : alice, 'tyler' : tyler }
并使用它:
for student_name in class_list:
student = students[student_name]
get_average(student)
total_class += get_average(student)
请注意,这样,可以在get_class_average(class_list)
内完全定义dicts,这可能是一个好主意。
答案 1 :(得分:0)
如果您需要按名称传递学生,请先写一个助手来完成从名称到实际学生数据对象的映射;你需要一个像注册表这样的小型数据库才能开始:
STUDENTS_BY_NAME = {'alice': alice, 'lloyd': lloyd, ...}
def get_students_by_name(names):
return [STUDENTS_BY_NAME[name] for name in names]
然后你可以这样做:
get_class_average(get_students_by_name(['alice', 'lloyd']))
修改get_class_average
以支持按名称传递学生会很难看,因为每个函数应该只做一件事,而且只有函数名称指定的东西。
最后,您可能只想立即以名称 - >数据映射格式声明学生:
STUDENTS = {
'alice': {...data here...},
'lloyd': {...data here...},
...
}
因此您不需要为每个学生提供这些中间变量,只需使用STUDENTS
中的get_students_by_name
注册表。
答案 2 :(得分:0)
我实际上遇到了同样的问题。我知道它不正确,因为我仍然收到错误。但这是我对Elazar的回答的解释:
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
results = []
# Add your function below!
def average(numbers):
total = sum(numbers)
total /= len(numbers)
return total
def get_letter_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
def get_average(students):
score = average(student["homework"] * 0.1) + average(student["quizzes"] * 0.3) + average(student["tests"] * 0.6))
def get_class_average(classs_list):
total = 0
for student in class_list:
student = student["name"]
get_average(student)
total_class += get_average(student)
return average_class
希望也许你可以从我对此的微弱尝试中学习。我在这个论坛上发布了我自己的问题,但由于我的假期情况,我不能真正倾向于这个问题。尽管如此,希望我的失败尝试基于我能得到的答案,可能对你有所帮助。