Hea*_* HA 2 python if-statement goto
所以我知道 goto 是一种非常糟糕的编码形式,但是当控制台中的输入不正确时,我需要一个程序来返回上一行。
print ("You wake up.")
print ("You do what?")
seg1 = input()
if seg1 == ("Stand") or seg1 == ("stand") or seg1 == ("stand up") or seg1 == ("Stand up") or seg1 == ("Stand Up"):
print ("You get up")
print ("You look around you... your in a dark room. A door hangs slightly ajar infront of you.")
print ("You do what?")
else:
print ("I dont understand")
Run Code Online (Sandbox Code Playgroud)
在 else 语句运行后,我希望它重复第 2 行并从那里继续执行程序……我该怎么做?
小智 7
Goto 语句通常用于非常低级的语言,如汇编或基本语言。在像python这样的高级语言中,它们被抽象出来,所以它们不存在。您想要这样做的方法是使用循环(这是 goto 语句的抽象)。这可以通过以下代码实现。
valid_input = False
while not valid_input:
print ("You wake up.")
print ("You do what?")
seg1 = input()
if seg1 == ("Stand") or seg1 == ("stand") or seg1 == ("stand up") or seg1 == ("Stand up") or seg1 == ("Stand Up"):
print ("You get up")
print ("You look around you... your in a dark room. A door hangs slightly ajar infront of you.")
print ("You do what?")
valid_input = True
else:
print ("I dont understand")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14813 次 |
| 最近记录: |