试图循环到一个函数的开始(排序)python

问题描述:

我想回到函数的顶部(不重新启动它,但转到顶部),但无法弄清楚如何做到这一点。而不是给你长的代码,我只是要弥补我想要的一个例子:试图循环到一个函数的开始(排序)python

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] 

我需要能够回去所以基本上很难忘记你试图使用“这里”无需再次擦拭记忆。虽然我知道它们很糟糕,但我基本上需要一个去,但它们不存在于python中。有任何想法吗?

*编辑:对不起,我忘记提及,当它已经被使用时,我需要能够选择其他地方去(我只是不想让代码停滞)。我把分数==“这一个”加了进去 - 所以如果我试图把它放在“这里”,“这里”已经被采用了,它会给我重做分数=输入(“”)的选择,然后我可以拿该价值并将其插入“这一个”而不是“这里”。你的循环语句会回到顶部,但不会让我把我刚刚找到的值放到其他地方。我希望这是决策意识:对

+4

使用'while'循环。 – 2013-04-27 20:04:31

+1

@AshwiniChaudhary +1你应该发布一个正确的答案。 – tripleee 2013-04-27 20:06:45

由于阿什维尼正确地指出,你应该做一个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") 
+1

嗯,你忘了在某处添加'end_condition = True'吗? ( - : – tripleee 2013-04-27 20:20:37

你所寻找的是一个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_placeTrue来停止循环。如果你还没有找到一个地方,found_place仍然False,你再次通过循环。