在我的代码的最后一行,我无法打印出结果

时间:2016-02-28 14:03:35

标签: python

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]
}

students = ['lloyd', 'alice', 'tyler']

for student in students:
    print student['name']

提醒的错误是str的指标只能是整数,而不是str。请帮我弄清楚我的错误

4 个答案:

答案 0 :(得分:6)

因为students包含恰好是学生姓名的字符串,而不是学生字典本身。变化:

students = ["lloyd", "alice", "tyler"] 

students = [lloyd, alice, tyler]

注意缺少报价。

答案 1 :(得分:1)

您创建了列表students作为字符串列表,而不是参考列表 您应该编辑以下内容:

students = ["lloyd", "alice", "tyler"]

使用对上面创建的dicts的引用:

students = [lloyd, alice, tyler]

答案 2 :(得分:0)

您的students变量是3个字符串的列表,而不是3个词典的列表

答案 3 :(得分:0)

你需要带走引号,这样就是:[lloyd, alice, tyler]

相关问题