0

最近我对 if else 语句有一个小问题。也就是说,我想创建一个函数,询问用户是否要读取脚本创建的文件,所以如果输入正确,函数会执行它的操作,但是当输入不正确时,我希望它再次恢复问题。

这是代码:

def read_the_file(output):
    print """
Do you want me to read your newly created file?
Type [Y]es or [N]o
    """
    question = raw_input("> ")
    reading = output.read()
    if question == 'yes'or question == 'Y' or question == 'y':
        print "BEGINNING OF FILE\n\n" + reading + "\n END OF FILE"
    elif question == 'no' or question == 'N' or question == 'n':
        sys.exit[1]
    else :
        print "wrong input"

read_the_file(output_file)

所以我想要这个函数做的是写

else:
    print "wrong input"

就是回去重演。

4

4 回答 4

0

您可以使用 while 循环来实现这一点。只要没有中断或 sys.exit,它就会返回开始,这意味着这里的每个错误输入。希望这可以帮助

def read_the_file(output):
        while True:
            print """
        Do you want me to read your newly created file?
        Type [Y]es or [N]o
            """
            question = raw_input("> ")
            reading = output.read()
            if question == 'yes'or question == 'Y' or question == 'y':
                print "BEGINNING OF FILE\n\n" + reading + "\n END OF FILE"
                break  # or sys.exit
            elif question == 'no' or question == 'N' or question == 'n':
                sys.exit[1]
            else :
                print "wrong input"

read_the_file(output_file)

但我建议稍微更改一下代码。现在每次读取文件时,无论您是否要读取它。您可以在用户说“是”后执行此操作。如果您使用该with语句,该文件将只为以下缩进部分打开。这里文件被读取。

 def read_the_file(output):
        while True:
            print """
        Do you want me to read your newly created file?
        Type [Y]es or [N]o
            """
            question = raw_input("> ")

            if question == 'yes'or question == 'Y' or question == 'y':
                # Open and read file here
                with open(output, 'r') as f:
                    reading = f.read()
                    # File is now closed
                print "BEGINNING OF FILE\n\n" + reading + "\n END OF FILE"
                break  # or sys.exit
            elif question == 'no' or question == 'N' or question == 'n':
                sys.exit[1]
            else :
                print "wrong input"

read_the_file(output_file)
于 2013-06-04T08:03:09.723 回答
0

尝试这个:

def read_the_file(output):

    reading = open(output, "rb").read()
    while True:
        question = raw_input("Do you want me to read your newly created file?\
                        Type [Y]es or [N]o :")
        if question in ["Yes","yes","YES", "y","Y"]:

            print "BEGINNING OF FILE\n\n" + reading + "\n END OF FILE"
            break

        elif question in ["NO", "no", "n", "N"]:
            sys.exit[1]

        else:
            print "wrong input"
于 2013-06-04T08:06:51.660 回答
0

这个怎么样

导入系统
定义 getUserResponse():
   打印 ”””
你想让我读你新创建的文件吗?
输入 [Y]es 或 [N]o
    """
   返回 raw_input("> ")

def read_the_file(输出文件):
   输出 = 打开(输出文件)

   问题 = getUserResponse()
   而(真):
      如果 question == 'yes' 或 question == 'Y' 或 question == 'y':
         阅读 = 输出.read()
         打印“文件开始\n\n”+阅读+“\n文件结束”
      elif question == 'no' 或 question == 'N' 或 question == 'n':
         输出.close()
         退出(1)
      别的:
         打印“输入错误”
      问题 = getUserResponse()
   输出.close()

read_the_file(sys.argv[1])
于 2013-06-04T08:17:26.457 回答
0

另一种方法是递归到同一个函数——这是一种“重试”的方法,它避免了将你的函数体包装成一个while循环。

更换你的线路

else:
    print "wrong input"

由线

else:
    print "wrong input. Try again..."
    read_the_file(output_file);
    return;

另外,请确保您return直接在递归之后。

当询问用户要写入的文件名时,我发现这个技巧很方便,然后在文件存在且不应被覆盖时询问新名称。

于 2013-06-04T08:38:50.693 回答