在python中尝试循环到函数的开头(某种程度上)
我试图返回函数的顶部(不是重新启动它,而是返回顶部),但不知道如何执行此操作。我不想给你冗长的代码,我只想给你一个我想要的例子:在python中尝试循环到函数的开头(某种程度上),python,function,loops,Python,Function,Loops,我试图返回函数的顶部(不是重新启动它,而是返回顶部),但不知道如何执行此操作。我不想给你冗长的代码,我只想给你一个我想要的例子: used = [0,0,0] def fun(): score = input("please enter a place to put it: ") if score == "this one": score [0] = total if score == "here" if used[1] == 0:
used = [0,0,0]
def fun():
score = input("please enter a place to put it: ")
if score == "this one":
score [0] = total
if score == "here"
if used[1] == 0:
score[1] = total
used[1] = 1
elif used[1] == 1:
print("Already used")
#### Go back to score so it can let you choice somewhere else.
list = [this one, here]
我需要能够回到过去,这样基本上它就忘记了你试图再次使用“这里”,而没有抹去记忆。尽管我知道它们很糟糕,但我基本上需要一个go to,但它们在python中并不存在。有什么想法吗
*编辑:啊,对不起,我忘了提到当它已经在使用时,我需要能够选择其他地方让它去(我只是不想让代码陷入困境)。我添加了分数==“this one”-因此,如果我尝试将其放在“here”,“here”中,它将给我重做score=input(“”)的选项,然后我可以将该值插入“this one”而不是“here”。您的循环语句将返回顶部,但不允许我将刚找到的值放在其他地方。我希望这是有意义的:正如Ashwini正确指出的那样,您应该在执行
循环时执行
def fun():
end_condition = False
while not end_condition:
score = input("please enter a place to put it: ")
if score == "here":
if used[1] == 0:
score[1] = total
used[1] = 1
elif used[1] == 1:
print("Already used")
正如Ashwini正确指出的,您应该执行while
循环
def fun():
end_condition = False
while not end_condition:
score = input("please enter a place to put it: ")
if score == "here":
if used[1] == 0:
score[1] = total
used[1] = 1
elif used[1] == 1:
print("Already used")
您要查找的是while
循环。您希望设置循环以继续运行,直到找到一个位置。大概是这样的:
def fun():
found_place = False
while not found_place:
score = input("please enter a place to put it: ")
if score == "here"
if used[1] == 0:
score[1] = total
used[1] = 1
found_place = True
elif used[1] == 1:
print("Already used")
这样,一旦找到一个位置,就可以将found\u place
设置为True
,从而停止循环。如果您没有找到一个位置,found\u place
将保持False
,然后再次执行循环。您要查找的是while
循环。您希望设置循环以继续运行,直到找到一个位置。大概是这样的:
def fun():
found_place = False
while not found_place:
score = input("please enter a place to put it: ")
if score == "here"
if used[1] == 0:
score[1] = total
used[1] = 1
found_place = True
elif used[1] == 1:
print("Already used")
这样,一旦找到一个位置,就可以将found\u place
设置为True
,从而停止循环。如果你还没有找到一个地方,found\u place
仍然是False
,然后你再次进行循环。@AshwiniChaudhary+1你应该把它作为一个正确的答案发布。@AshwiniChaudhary+1你应该把它作为一个正确的答案发布。呃,你忘了在什么地方添加end\u condition=True
?(:呃,您是否忘记在某处添加end\u condition=True
)(: