我相當落後於我的課程,我停留在發展我的代碼任務2 我的程序必須允許用戶輸入一系列的條形碼,搜索文本文件的代碼,然後如果找到,則詢問用戶他們希望購買的產品的數量。最後它應該打印收到的總產品和總價格。搜索文本文件的字符串
但我的代碼不會當我在我的文件中搜索代碼的工作,它只是不斷循環,並要求用戶重新輸入其條碼。
這是到目前爲止我的代碼:
loop=True
while loop==True:
print ("------STOCK LIST------")
print ("a - Place an order by barcode")
print ("b - Place an order by product name")
print ("x - Exit")
task=input("Please make your selection\n")
if task.lower()=="a":
print("------STOCK FILE LOADING------")
myfile=open("barcode.txt", "r") #this opens the text file
details=myfile.readlines() #reads the file and stores it as the variable 'details' #myfile.close() #closes the file
while True:
digits=input("Please enter your GTIN-8 code\n")
if len(digits) > 8 or len (digits) < 8: #if the digits are longer or shorter than 8 digits, the code is not accepted
print("Please enter a GTIN-8 code\n")
else:
break #if the code is the correct length, the loop ends
for line in details:
if digits in line:
productline=line
myfile=open("receipt.txt", "r") #opens receipt file
myfile.writelines("\n" + "+")
quantity=input("How much of the product do you wish to purchase?\n")
itemsplit=itemline.split(' ') #seperates into different words
price=float(itemsplit[2]) #price is
total=(price)*(quantity) #this works out the price
myfile.writelines("Your total spent on this product is: " +str("£:,.2f)".format(total)+"\n"))
else:
break
如果你能幫助我,我將非常感激因爲沒有我的同學都會幫助我,如果是這樣,你可以保持的代碼,因爲我很簡單不是最好的編碼?
當你檢查條形碼是正確長度的突破後,你跳出while循環也就是說你的代碼的其餘部分是無法運行 –