How do you get python to read a whole text file, not just one line? -


i've been trying code search text file product entered user reads first line, not whole file want to.

here code:

 order=input("please enter name of product wish purchase\n")     myfile=open("barcode.txt","r")     details=myfile.readlines() #reads file , stores variable 'details'     line in details:         if order in line: #if barcode in line stores line 'productline'             productline=line             quantity=int(input("how of product wish purchase?\n"))             itemsplit=productline.split(' ') #seperates different words             price=float(itemsplit[1]) #the price second part of line             total=(price)*(quantity) #this works out price             print("your total spent on product is: " +'£'+str(total))         else:             break 

your breaking out of loop:
(btw added statent opening file in more pythonic way)

order = input("please enter name of product wish purchase\n") open("barcode.txt","r") myfile:     details=myfile.readlines() #reads file , stores variable 'details'     line in details:         if order in line: #if barcode in line stores line 'productline'             productline=line             quantity=int(input("how of product wish purchase?\n"))             itemsplit=productline.split(' ') #seperates different words             price=float(itemsplit[1]) #the price second part of line             total=(price)*(quantity) #this works out price             print("your total spent on product is: " +'£'+str(total)) 

Comments