Python 2 - 如何使脚本转到特定的行?

use*_*147 1 python

这是我上一个问题的扩展,但我想知道是否有可能使脚本返回到特定的行,如果事件发生.

print "Type in 'Hello'"
typed = raw_input("> ")
if typed.lower() in ['hello', 'hi']:
    print "Working"
else:
    print "not working" 
Run Code Online (Sandbox Code Playgroud)

在最后一行,如果'else'发生了,你可以做到这样它再次从第2行重新开始吗?非常感谢任何帮助,非常感谢!

hal*_*lex 6

您可以将代码置于无限循环中,只有输入正确的单词才会退出:

print "Type in 'Hello'"
while True:
    typed = raw_input("> ")
    if typed.lower() in ['hello', 'hi']:
        print "Working"
        break
    else:
        print "not working" 
Run Code Online (Sandbox Code Playgroud)