2013-04-27 46 views
0

我想回到函數的頂部(不重新啓動它,但轉到頂部),但無法弄清楚如何做到這一點。而不是給你長的代碼,我只是要彌補我想要的一個例子:試圖循環到一個函數的開始(排序)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

回答

1

由於阿什維尼正確地指出,你應該做一個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

5

你所尋找的是一個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,你再次通過循環。