gpt4 book ai didi

python - 我想找到一个列表的总数,但是出现错误

转载 作者:行者123 更新时间:2023-11-28 22:38:47 25 4
gpt4 key购买 nike

我想求cost的总和,但是在我加上[30]之后,出现了错误信息。这是我的代码的一部分:

cost = [['Lounge', 70], ['Bedroom', 70], ['Bathroom', 70], [30]]
print("£", sum(c[1] for c in cost))

这是错误消息之前出现的内容:

Cost: [['Lounge', 70], ['Bedroom', 70], ['Bathroom', 70], [30]]
Total cost:

这是出现的错误消息:

Traceback (most recent call last):
File "G:\Dell download may15\Documents\Qadir's file\Putteridge High School\GCSE years\Year 11\Computing\Coding\Python\CA folder\customer.py", line 93, in <module>
print("£", sum(c[1] for c in cost))
File "G:\Dell download may15\Documents\Qadir's file\Putteridge High School\GCSE years\Year 11\Computing\Coding\Python\CA folder\customer.py", line 93, in <genexpr>
print("£", sum(c[1] for c in cost))
IndexError: list index out of range

最佳答案

列表中的最后一个列表没有第二个项目。

关于python - 我想找到一个列表的总数,但是出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391891/

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