[英]Unsupported Operand Types using pop()
我試圖讓它輸入我的雜貨的價值,它給了我像收銀機一樣的小計,特別是使用pop()
function。 但是,我的代碼總是在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)
回溯(最近一次調用最后一次):第 16 行,在小計 = 小計 + purchase_amounts.pop() 類型錯誤:+ 不支持的操作數類型:'int' 和 'str'
代替:
int(price)
(purchase_amounts.append(price))
你應該有:
purchase_amounts.append(int(price))
int
返回str 到 int 轉換的結果,不會就地更改其參數。 哦,還有這個:
while purchase_amounts != [ ]:
應改為:
while purchase_amounts:
– 非空列表是真實的……
只需將int(price)
更改為price = int(price)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.