【发布时间】:2020-07-27 11:51:53
【问题描述】:
我正在尝试让我输入我的杂货的价值,它会像收银机一样给我小计,特别是使用pop() 函数。但是,我的代码总是在subtotal = subtotal + purchase_amounts.pop() 返回一个操作数错误。我没有正确地将值更改为int?
purchase_amounts = []
price = []
subtotal = 0
while price != "done":
price = input("How much did groceries cost? If done, type 'done'")
if price != "done":
int(price)
(purchase_amounts.append(price))
print(purchase_amounts)
while purchase_amounts != [ ]:
subtotal = subtotal + purchase_amounts.pop()
print(subtotal)
Traceback(最近一次调用最后一次): 第 16 行,在 subtotal = subtotal + purchase_amounts.pop() TypeError: unsupported operand type(s) for +: 'int' and 'str'
【问题讨论】:
-
请将完整的错误回溯添加到您的问题中。
标签: python python-3.x