gpt4 book ai didi

python - 如何解决这个类型错误: string indices must be integers?

转载 作者:太空宇宙 更新时间:2023-11-03 19:22:28 29 4
gpt4 key购买 nike

我是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

编辑:谢谢,我通过调整方法的调用方式使其工作。

a={ 'uen': 's1234567h', 'AGE2011':21, 'INC2011':100, 'SAV2011':80}
b={ 'uen': 's1234567h', 'AGE2011':28, 'INC2011':300, 'SAV2011':100}
x=[a,b]
calculate_average_expenditure(x)

最佳答案

我猜参数a绑定(bind)到一个字符串列表。您能否展示如何调用 calculate_average_expenditure(...) 以及 ... 包含哪些内容?

关于python - 如何解决这个类型错误: string indices must be integers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9241834/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com