如何解决这个类型错误:字符串索引必须是整数?

2024-10-02 16:23:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,可能不太熟悉详细的语法规则。在

我能知道如何解决这个错误吗?它指向6、7、8号线,我不确定出了什么问题。谢谢!在

def calculate_average_expenditure(a):
    totalincome = 0
    totalsavings = 0
    count = 0
    for item in a:
        if (item['AGE2011'] >= 20 and item['AGE2011'] <= 30):
            totalincome += item['INC2011']
            totalsavings += item['SAV2011']
            count += 1

    totalexpenditure = totalincome-totalsavings
    return totalexpenditure/count

编辑: 谢谢,我通过调整方法的调用方式使它工作起来。在

^{pr2}$

Tags: 规则defcount错误语法item指向average