简体   繁体   English

在文本文件中搜索字符串

[英]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. 我的课程工作非常落后,我不得不为任务2开发我的代码。我的程序必须允许用户输入一系列条形码,在文本文件中搜索代码然后,如果找到,请询问用户他们想要购买的产品数量。 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. 我现在正在做GCSE,所以我很欣赏课程的困难。 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) 1.缩进不正确 (如果我正确理解你要做的事情)

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. 现在,如果用户输入的代码是正确的,那么一旦验证代码,就会打破整个while循环。 The program does not have time to do the next steps in the for loop. 该程序没有时间在for循环中执行后续步骤。

2. The second if statement 2.第二个if语句

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 . 您可以改写一个新的if语句,以便仅在输入的代码长度正确时才打破while循环 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! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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