小编Roo*_*ner的帖子

错误:列表索引必须是整数而不是浮点数

下面的代码应该从学生的字典中获取标记列表并计算学生的平均分数.我得到"TypeError:list indices必须是整数,而不是float"错误.

alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}

# Averege function is given below for calculating avg
def average(lst):
    l=float(len(lst))
    total = 0.0
    #code works till here the error occoured below
    for item in lst:
        add = int(lst[item])
        print add
        total+=add
    return total//l

print average(alice['tests'])
print alice['tests']
Run Code Online (Sandbox Code Playgroud)

python python-2.7

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×1

python-2.7 ×1