2016-11-28 89 views
0

我的程序的循环没有完全工作,似乎有一个逻辑错误的地方它打印出的结果更那么一次,我希望它只是打印出结果一次。我研究了很多,但找不到我的答案,这是我的代码。环路故障

import csv 
while True: 
    code=input('Enter the GTIN-8 code of the product youn would like to purchase or type done if you would like to discontinue') 
    if code=='done': 
     break 
    digitlength=len(code) 
    if digitlength==8: 
     CSV1=open('CSV1.csv', 'rt') 
     CSV1_contents=csv.reader(CSV1) 
     CSV1_blank=open('CSV1_blank.csv', 'wt') 
     CSV1_blank_contents=csv.writer(CSV1_blank) 
     for row in CSV1_contents: 
      for field in row: 
       if field==code: 
        quantity=int(input('How many of the products do you want to purchase')) 
        for code in CSV1: 
         currentstockAFO=int(row[2])-quantity 
         if currentstockAFO<=int(row[3]): 
          GTIN8=row[0] 
          nameproduct=row[1] 
          levelreorder=row[3] 
          targetstock=int(row[4]) 
          amountofreorder=targetstock-currentstockAFO 
          CSV1_blank_contents.writerows([[GTIN8,nameproduct,amountofreorder,levelreorder,targetstock]]) 
          print('GTIN-8 code of your product :'+GTIN8+'\n'+'The products name :'+nameproduct+'\n'+'The quantity of the product to be reordered :'+str(amountofreorder)+'\n'+'Re-order level :'+row[3]+'\n'+'Target stock level :'+row[4]+'\n') 
    else: 
     print('Error! The GTIN-8 code you have entered is invalid! Please type again\n') 
CSV1.close() 
CSV1_blank.close() 

这是什么输出:

Enter the GTIN-8 code of the product youn would like to purchase or type done if you would like to discontinue94796001 
How many of the products do you want to purchase6 
GTIN-8 code of your product :94796001 
The products name :cheese     
The quantity of the product to be reordered :29 
Re-order level :3 
Target stock level :30 

GTIN-8 code of your product :94796001 
The products name :cheese     
The quantity of the product to be reordered :29 
Re-order level :3 
Target stock level :30 

GTIN-8 code of your product :94796001 
The products name :cheese     
The quantity of the product to be reordered :29 
Re-order level :3 
Target stock level :30 

GTIN-8 code of your product :94796001 
The products name :cheese     
The quantity of the product to be reordered :29 
Re-order level :3 
Target stock level :30 

GTIN-8 code of your product :94796001 
The products name :cheese     
The quantity of the product to be reordered :29 
Re-order level :3 
Target stock level :30 

GTIN-8 code of your product :94796001 
The products name :cheese     
The quantity of the product to be reordered :29 
Re-order level :3 
Target stock level :30 

我不明白如何使输出只能打印一次,谢谢你在前进

回答

0

也许尝试删除for code in CSV1

你的新代码应该是这样的:

import csv 
while True: 
    code=input('Enter the GTIN-8 code of the product youn would like to purchase or type done if you would like to discontinue') 
    if code=='done': 
     break 
    digitlength=len(code) 
    if digitlength==8: 
     CSV1=open('CSV1.csv', 'rt') 
     CSV1_contents=csv.reader(CSV1) 
     CSV1_blank=open('CSV1_blank.csv', 'wt') 
     CSV1_blank_contents=csv.writer(CSV1_blank) 
     for row in CSV1_contents: 
      for field in row: 
       if field==code: 
        quantity=int(input('How many of the products do you want to purchase')) 
        currentstockAFO=int(row[2])-quantity 
        if currentstockAFO<=int(row[3]): 
         GTIN8=row[0] 
         nameproduct=row[1] 
         levelreorder=row[3] 
         targetstock=int(row[4]) 
         amountofreorder=targetstock-currentstockAFO 
         CSV1_blank_contents.writerows([[GTIN8,nameproduct,amountofreorder,levelreorder,targetstock]]) 
         print('GTIN-8 code of your product :'+GTIN8+'\n'+'The products name :'+nameproduct+'\n'+'The quantity of the product to be reordered :'+str(amountofreorder)+'\n'+'Re-order level :'+row[3]+'\n'+'Target stock level :'+row[4]+'\n') 
    else: 
     print('Error! The GTIN-8 code you have entered is invalid! Please type again\n') 
CSV1.close() 
CSV1_blank.close() 
+0

不错的工作伙计。 – BILLI