0

我有一个用 Python 2.7 编写的简单代码,它会询问用户某些信息,并将信息导出到 .csv 文件。一旦用户输入数据,他们有没有办法在按下回车后返回并编辑他们输入的内容?这是我到目前为止所拥有的:

def writer():
import csv

with open('Work_Order_Log.csv', 'a') as f:
    w=csv. writer(f, quoting=csv.QUOTE_ALL)

    while (1):
        Correct=True
        Employee=True
        WorkOrder=True
        Item=True
        Qty=True
        Process=True
        Date=True
        Time=True
        while Correct:
            Correct=False
            Employee=False
            WorkOrder=False
            Item=False
            Qty=False
            Process=False
            Date=False
            Time=False
            Employee=raw_input("1. Enter Your Name:")
            WorkOrder=raw_input("2. Enter The Work Order Number:")
            PartNumber=raw_input("3. Enter The Item Number:")
            Qty=raw_input("4. Enter Quantity:")
            Process=raw_input("5. Enter Process:")
            Date=raw_input("6. Enter Date(mm/dd):")
            Time=raw_input("7. Total Time(hh:mm):")
            needToCorrect=raw_input("Is the last Entry Correct? (If so, type 'y') If not enter the Number of the Field that is incorrect:")
            if needToCorrect=="1":
                Employee=True
            elif needToCorrect=="2":
                WorkOrder=True
            elif needToCorrect=="3":
                Item=True
            elif needToCorrect=="4":
                Qty=True
            elif needToCorrect=="5":
                Process=True
            elif needToCorrect=="6":
                Date=True
            elif needToCorrect=="7":
                Time=True
            w.writerow([Employee,WorkOrder,Item,Process,Qty,Date,Time,Correct])

writer()

在测试代​​码后,我发现当我输入错误字段的编号进行更正时,它在 .csv 文件中显示它不正确,但仍然让我通过整个循环来修复错误。为什么是这样?

4

1 回答 1

0

您可以将所有输入放入 while 块中:

while (1):
    Correct = True
    while Correct:
        Correct = False
        Employee=raw_input("Enter Your Name:")
        ...

        needToCorrect=raw_input("Is the last Entry Correct?(y/n):")
        if needToCorrect == "n": 
             Correct = True
    w.writerow([Employee,WorkOrder,PartNumber,Process,Qty,Date,Time,Correct])

然后,如果用户发现某些内容不正确,“n”将提示用户返回并重新键入字段。如果您只想更正某些字段,类似的、更复杂的方法可以正常工作。

于 2013-08-12T18:13:40.880 回答