使用相同的变量重新提示用户的输入

时间:2014-06-03 19:26:26

标签: python python-3.x

是否可以在以下代码的continue_or_exit中调用else:变量?:

continue_or_exit = input('Would you like to "continue" or "exit"? ')
if continue_or_exit == 'continue':
   task3(word_lines, clueslistl, clueslists, clues)
elif continue_or_exit == 'exit':         
    quit()
else:

如果没有,是否有另一种方法,最好不需要功能?

2 个答案:

答案 0 :(得分:2)

喜欢这个吗?

while True:
    continue_or_exit = input('Would you like to "continue" or "exit"? ')
    if continue_or_exit == 'continue':
       task3(word_lines, clueslistl, clueslists, clues)
    elif continue_or_exit == 'exit':         
        quit()

答案 1 :(得分:0)

您可以使用while循环而不是if语句:

while True:
    continue_or_exit = input('Would you like to "continue" or "exit"? ')
    if continue_or_exit == 'continue':
       task3(word_lines, clueslistl, clueslists, clues)
    elif continue_or_exit == 'exit':         
        break
    else:
        print("Input not recognized, please try again.\n")
quit()

这应该保持循环并询问输入,直到用户选择退出,尽管这是未经测试的。