所以我正在开发一个代码,让用户输入产品的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)
答案 0 :(得分:1)
这里,当您从文件中获取值时,它们是字符串类型:
quantity=currentline[3]
ItemPrice=currentline[2]
您需要在乘法之前将它们转换为int,例如:
quantity=int(currentline[3])