"-6ren">
gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:10:49 25 4
gpt4 key购买 nike

这是对我的 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"

关于Python 2 - 如何使脚本转到特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12077977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com