【发布时间】:2017-09-02 03:35:08
【问题描述】:
所以我正在开发一个代码,让用户输入产品的 GTIN-8 代码,输入他们想要的金额,然后提供收据。但是,当我到达代码的最后一部分时,我试图将产品的数量和价格相乘,我得到了“不能将序列乘以非 int 类型的‘str’错误。 以下是部分代码:
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是整数,但它们不是。 -
检查
quantity和ItemPrice;它们长什么样子,有哪些类型?
标签: python