有没有办法返回并编辑用户输入的信息?

时间:2013-08-12 17:54:40

标签: python-2.7

我有一个用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文件中显示它不正确,但仍然让我通过整个循环来修复错误。这是为什么?

1 个答案:

答案 0 :(得分: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”将提示用户返回并重新键入字段。如果您只想更正某些字段,则类似的,更复杂的方法可以正常工作。