重新启动if then语句

Joh*_*ith 2 python if-statement

就像我在python中一样.

choice1 = raw_input('John Blue Green')
if choice1 == 'A':
   print('blah')
elif choice1 == 'B':
   print('blahblah')
Run Code Online (Sandbox Code Playgroud)

有人进入B但不正确所以我希望它回去再问一次.我怎么做?当心,我是一个编程菜鸟.

pok*_*oke 5

你基本上需要循环这个.一个例子是将它置于无限循环中,并在达到所需结果时手动将其分解:

while True:
    choice1 = raw_input('John Blue Green')
    if choice1 == 'A':
        print('blah')
        break # <-- 'A' is okay, so we can get out of the loop then
    elif choice1 == 'B':
        print('blahblah')
Run Code Online (Sandbox Code Playgroud)

根据您的情况,您当然可以调整True循环条件,使其不是无限的,但实际上会对用户输入做出反应.然后你就不需要了break,但循环会自然地停止循环.但是如果你有多个接受输入值,那么仍然使用中断而不是具有巨大的循环条件可能更清晰.