我想找到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/