简体   繁体   中英

Searching a text file for a string

I am quite behind on my coursework and I am stuck on developing my code for task 2. My program must allow a user to enter a series of bar codes, search a text file for the codes and then, if found, ask the user for the quantity of the product they wish to purchase. Finally it should print a receipt of the total products bought and the total price.

However, my code does not work when I have to search for the code in my file, it just keeps looping and asking the user to enter their bar code again.

Here is my code so far:

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

If you could help me I would be very grateful as none of my classmates will help me and if so, can you keep the code quite simple as I am not the best at coding?

I'm doing GCSEs myself right now so I appreciate the difficulty with coursework. Careful asking people for code though - I'm pretty sure that can get you disqualified in certain specifications (although I'm not doing the same as you).

I see two main problems in your code:

1. The indentation is incorrect (if I've correctly understood what you're trying to do)

I think it should be like this:

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

Right now, if the code inputted by the user is correct, you're breaking out of the entire while loop as soon as the code is validated. The program does not have time to do the next steps in the for loop.

2. The second if statement

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:

This can be made better. You can write a new if statement instead so that it breaks the while loop only if the code entered is the correct length . Your current statement asks me to re-input the code twice every time I get it wrong, rather than only asking me once each time I input something incorrect.

I'm about to go work on your other question.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM