回路故障

时间:2016-11-28 15:08:39

标签: csv python-3.2

我的程序的循环不能完全正常工作,似乎有一个逻辑错误,它打印出结果多一次,我希望它只打印一次结果。我已经研究了很多,但找不到我的答案,这是我的代码。

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

我不明白如何只输出一次输出,谢谢你提前

1 个答案:

答案 0 :(得分: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()