2016-11-28 4 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