2016-04-03 104 views
1

我相当落后于我的课程,我停留在发展我的代码任务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 

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

+0

当你检查条形码是正确长度的突破后,你跳出while循环也就是说你的代码的其余部分是无法运行 –

回答

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: 

这可以变得更好。代替它打破了while仅环如果输入的密码是正确的长度你可以写一个新的if声明。 您当前的声明会要求我在每次出错时重新输入代码两次,而不是每次输入错误的时候都会询问一次。

我即将开始处理您的其他问题。

祝你好运!

+0

太感谢你了,我已经工作好几个星期的和不知道问题是什么。 – EmDuff

+0

是的,你打开它为'r'(只读)。我认为'r +'就是你正在寻找的P.S.如果你“接受”了答案(如果它有你需要的一切),那么其他人不会浪费时间回答它 –

+0

它可以工作,但它不会在文件中写任何东西,我认为这是浮标位 – EmDuff