2016-11-28 13 views
0

私のプログラムのループは完全には機能しません。結果が1回以上印刷される場所には論理的なエラーがあるようですが、結果を1回だけ印刷します。私は多くを研究したが、私の答えを見つけることができなかった、これは私のコードです。ループの不具合

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