在文本文件中搜索字符串

时间:2016-04-03 15:24:19

标签: python

我在课程上非常落后,我不得不为任务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

如果你能帮助我,我将非常感激,因为我的同学都不会帮助我,如果是这样,你能不能保持代码非常简单,因为我不是最好的编码?

1 个答案:

答案 0 :(得分:0)

我现在正在自己做GCSE,所以我很欣赏课程的困难。小心地要求人们提供代码 - 我非常确定可以让你在某些规范中被取消资格(尽管我并没有像你那样做)。

我在您的代码中看到两个主要问题:

<强> 1。压痕不正确(如果我已正确理解您正在尝试做什么)

我认为它应该是这样的:

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循环。该程序没有时间在for循环中执行后续步骤。

<强> 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:

这可以做得更好。您可以改为编写新的if语句,只有在输入的代码长度正确时才会中断while循环。 你当前的陈述要求我每次出错都要重新输入两次代码,而不是每次输入错误时只问我一次。

我即将开始处理你的另一个问题。

祝你好运!