Skip to main content
 首页 » 编程设计

Python 2之如何使脚本转到特定行

2025年05月04日10myhome

这是对我的 last question 的一种扩展, 但我想知道如果事件发生,是否可以让脚本返回到特定行。

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

在最后一行,如果发生“else”,你能不能让它再次从第 2 行重新开始? 非常感谢任何帮助,非常感谢!

请您参考如下方法:

你可以把你的代码放在一个无限循环中,只有在输入正确的单词时才会退出:

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