2017-04-06 30 views
0

所以我正在研究一個代碼,它允許用戶輸入產品的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) 
+0

請發佈您從Python獲得的確切錯誤逐字。 –

+0

你認爲'quantity'和'ItemPrice'是整數,但它們不是。 – Junuxx

+0

檢查'quantity'和'ItemPrice';它們是什麼樣子的,它們是哪一種? – Cleb

回答

1

在這裏,當你從文件中得到了價值,他們是一個字符串類型:

quantity=currentline[3] 
ItemPrice=currentline[2] 

你需要將它們轉換乘以之前爲int,例如:

quantity=int(currentline[3]) 
+0

如[回答]中所述,請避免回答不清楚,過於寬泛,錯字,基於意見的,不可重複的或重複的問題。編寫我的代碼請求和費力的家庭作業問題不適用於[所以],更適合於專業編碼/輔導服務。良好的問題堅持[問],包括[mcve],有研究的努力,並有潛力對未來的訪問者有用。回答不適當的問題會使網站變得更難以瀏覽和鼓勵更多這樣的問題,從而損害其他用戶的志願者時間和專業知識。 – TigerhawkT3

相關問題