我有一個代碼打開文本文件並詢問用戶是否要輸入gtin-8產品代碼。但是我找不到該產品的總成本。任何幫助將不勝感激!這是我的文本文件和代碼...從用戶請求的文本文件讀取和寫入
文本文件:
gtin8 name cost
34512340 plain brackets £0.50
56756777 100 mm bolts £0.20
90673412 L-shaped brackets £1.20
76842309 Screwdriver £3.00
6 Radiator Key £4.00
34267891 Panel Saw £12.00
67532189 Junior Hacksaw £7.00
98123470 Wrench £8.00
18653217 Shovel £8.00
67543891 Hammer £10.00
23879462 File £7.00
我的代碼:
loop = True
productsfile = open("Products.txt", "r+")
recieptfile = open("Receipt.txt", "w")
search = productsfile.readlines()
while True:
yesno = input("Would you like to type in a gtin-8 product code?(yes/no)")
if yesno == "yes":
gtin8 = int(input("Please enter your GTIN-8 code: "))
if len(gtin8) == 8:
while True:
for line in search:
if gtin8 in line:
productline = line
recieptfile = open("Receipt.txt", "w")
recieptfile.writelines("\n" + "+")
quantity = int(input("What is the quantity of the product you require? "))
itemsplit = productline.split(",")
cost = float(itemsplit[3])
totalcost = (cost)*(quantity)
recieptfile.writelines("Your total cost is: ", totalcost)
else:
print("Here is your reciept", "reciept.txt", "r")
'if len(gtin8)== 8:'不能正確,因爲gtin'是一個整數。 –
這個問題不是固定的,但它給了我一個錯誤,說這個...回溯(最近一次調用最後一次): 文件「N:\年10-11 \ Computing \ A453 \受控評估任務2 \受控評估Code.py 「,第17行,在 cost = float(itemsplit.split(」,「)) AttributeError:'list'對象沒有屬性'split' –