這是我的代碼。我想添加列表總成本,所以代碼將返回140英鎊,但它不起作用。我希望英鎊符號位於成本總和的前面,以便用戶知道程序顯示的內容。爲什麼python中的求和函數不工作?
cost = [['Lounge', 70], ['Kitchen', 70]]
print(cost)
print("£", sum(cost))
它返回此錯誤消息:
TypeError: unsupported operand type(s) for +: 'int' and 'list'
我在網上看了看,但沒有這些結果的幫助我:
sum a list of numbers in Python
How do I add together integers in a list in python?
Sum the second value of each tuple in a list
擊敗我吧。 +1 – intboolstring
謝謝,它的工作:)但你能解釋它,讓我明白它是如何工作的? – User0123456789
這是'lis = [] for c in cost的快捷方式:lis.append(c [1])print(「£」,sum(lis))''。 [這裏](https://wiki.python.org/moin/Generators)是關於生成器的更多信息。 – zondo