所以我正在研究一個代碼,它允許用戶輸入產品的GTIN-8代碼,輸入他們想要的數量,然後給出收據。但是,當我到達代碼的最後一部分時,我試圖乘以產品的數量和價格,我得到'不能乘以非int類型'str'錯誤的序列。 下面的代碼的一部分:Python - '不能通過類型爲'str'的非int類型的序列來增加序列錯誤?
while IfFinished != "Yes":
ProductsWanted=input("Please enter the GTIN-8 Code of the product: ")
AmountOfProducts=input("How many do you want? ")
with open("Productsfile.txt") as f:
for line in f:
if ProductsWanted in line:
Receipt=open("ReceiptFile.txt","a")
Receipt.write("%r, %r, \n" % (line, AmountOfProducts))
Receipt.close()
if ProductsWanted not in ["23456945","12376988","76543111","92674769","43125999"]:
print("Product not found")
else:
print("Product found")
IfFinished=input("Are you done? If so, type 'Yes' ")
if IfFinished == "Yes":
print("Thank you for shopping with us!")
else:
print("Please continue")
Receipt=open("ReceiptFile.txt","r")
print(Receipt.read())
Receipt.close()
with open("ReceiptFile.txt","r") as file:
for line in file:
currentline=line.split(",")
quantity=currentline[3]
ItemPrice=currentline[2]
Totalprice=quantity*ItemPrice
price="0"
Total=price + Totalprice
print(Total)
請發佈您從Python獲得的確切錯誤逐字。 –
你認爲'quantity'和'ItemPrice'是整數,但它們不是。 – Junuxx
檢查'quantity'和'ItemPrice';它們是什麼樣子的,它們是哪一種? – Cleb