這是我的數據庫的代碼,我已經使用了一個文本文件,並將其插入到代碼中,但是當它到達第27行時,它出現了錯誤股票文件,接受訂單,併產生一個列表或產品購買
Traceback (most recent call last):
File "N:\computing\databse\task 2.py", line 27, in <module>
print("you bought", + products[i][1], "at a cost of", +str(products_total))
TypeError: bad operand type for unary +: 'str'
我試着看過很多不同的網站,但我無法弄清楚如何解決這個問題或者代碼有什麼問題,爲什麼它不起作用!其他的一切似乎是工作的罰款,如果有人可以幫助我一點點我將非常感謝:
products = []
with open('StockFile.txt','r') as i:
for line in i:
line = line[:-1]
row = line.split(',')
products.append(row)
print(products)
total = 0
items = []
answer = "yes"
while answer == "yes":
GTIN = input ("Please input GTIN:")
found = False
for i in range (0,len(products)):
if GTIN == products[i][0]:
found = True
items.append(GTIN)
items.append(products[i][1])
items.append(products[i][2])
quantity = input("How many would you like?")
items.append(quantity)
product_total = int(quantity) * float (products[i][2])
items.append(product_total)
print("you bought", + products[i][1], "at a cost of", +str(products_total))
total = total + products_total
if found == False:
print("Sorry not a valid number try again")
print("DO you want another item?")
answer = input()
for i in range(0,len(items),5):
print(items[i], items[i+1], items[i+2], items[i+3], items[i+4])
print("total cost of the order is £" +str(total))
這裏是我使用的文本文件:
13245627,螺母和螺栓,0.5 34512340,平原括號,1個 56756777,100mm螺栓,2.5 90673412,L形的托架,0.7