回路故障

Loop malfunction

我的程序的循环没有完全运行,它多次打印结果的地方似乎存在逻辑错误,我希望它只打印一次结果。我研究了很多但找不到我的答案,这是我的代码。

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

不明白怎么让输出只打印一次,先谢谢了

也许尝试删除 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()