-1

为什么 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
4

3 回答 3

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)

请注意,这样,dicts 可以完全在内部定义get_class_average(class_list),这可能是一个好主意。

于 2013-09-27T11:13:37.210 回答
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

希望也许你能从我在这方面的微弱尝试中吸取教训。我在这个论坛上发布了我自己的问题,但由于我的假期情况,我无法真正解决这个问题。尽管如此,希望我失败的尝试基于我能得到的很少的答案可能会对你有所帮助。

于 2014-07-20T06:19:56.273 回答
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因为每个函数都应该做一件事,并且只做函数名称指定的事情。

最后,您可能只想立即以 name->data 映射格式声明学生:

STUDENTS = {
    'alice': {...data here...},
    'lloyd': {...data here...},
    ...
}

所以你不需要每个学生的这些中间变量,并且可以只STUDENTS使用get_students_by_name.

于 2013-09-27T11:21:32.330 回答