How to repeat function in Python depending on output?
本问题已经有最佳答案,请猛点这里访问。
最近我对if else语句有一个小问题。 即我想创建一个函数,询问用户输入是否要读取脚本已创建的文件,因此如果输入正确,则函数会执行其操作然而当输入不正确时我希望它再次回复问题。
这是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 " + reading +" END OF FILE" elif question == 'no' or question == 'N' or question == 'n': sys.exit[1] else : print"wrong input" read_the_file(output_file) |
所以我想要的功能是写作
1 2 | else: print"wrong input" |
是回去重复自己。
另一种方法是递归到同一个函数 - 这是一种"再试一次"的方法,它避免了将函数体包装到
替换你的线
1 2 | else: print"wrong input" |
按行
1 2 3 4 | else: print"wrong input. Try again..." read_the_file(output_file); return; |
另外,请确保在递归后直接
当我向用户询问要写入的文件名时,我发现这个技巧很方便,如果该文件存在且不应该被覆盖,则会要求新名称。
这个怎么样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import sys def getUserResponse(): print""" Do you want me to read your newly created file? Type [Y]es or [N]o """ return raw_input(">") def read_the_file(output_file): output = open(output_file) question = getUserResponse() while (True): if question == 'yes' or question == 'Y' or question == 'y': reading = output.read() print"BEGINNING OF FILE " + reading +" END OF FILE" elif question == 'no' or question == 'N' or question == 'n': output.close() exit(1) else: print"wrong input" question = getUserResponse() output.close() read_the_file(sys.argv[1]) |
试试这个:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 " + reading +" END OF FILE" break elif question in ["NO","no","n","N"]: sys.exit[1] else: print"wrong input" |
您可以使用while循环实现此目的。只要没有中断或sys.exit,它就会返回到开始,这意味着每次输入错误的输入。希望这可以帮助
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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 " + reading +" 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) |
但我建议稍微改变一下代码。现在每次读取文件时,无论您是否想要读取它。你可以在用户说"是"之后这样做。如果使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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 " + reading +" 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) |