2012-01-13 44 views
2

我一直在嘗試一段時間才能訪問我最近返回的值,並在if語句中使用它,而不必調用該值。訪問以前返回的值 - Python3.2

基本上我有一個while循環調用一個函數,允許用戶輸入,然後將輸入返回到循環中。

while selection() != 0: ## Calls the "WHAT WOULD YOU LIKE TO DO" list and if it is 0 quits the script 
    input() ## just so it doesn't go straight away 
    if selection.return == 1: ## This is what I would like to happen but not sure how to do it... I've googled around a bit and checked python docs 

見,如果我把:

if selection() == 1: 

它會工作,但再次顯示「你會喜歡做的事」的文字...

對不起,如果這是一個明顯的解決方案,但幫助將非常感謝:)

+2

你必須使用一個變量。所以,雖然真:sel = selection();如果sel == 0:break;否則:#做任何你做的事 – AdamKG 2012-01-13 19:41:05

回答

8

這就是爲什麼你會將結果存儲在一個變量,所以你可以在將來參考它。喜歡的東西:

sel = selection() 
while sel != 0: 
    input() 
    if sel==1: 
     ... 
    sel = selection() 
+0

哦,這使得很多感覺哈哈!謝了哥們! – Clement 2012-01-13 19:51:41

3

這只是張貼的答案(實在是太尷尬加入了註釋)的選擇,但請不要改變你的答案:)無論你是否喜歡它更好有點可以選擇偏好,但我喜歡不必重複輸入源代碼行,儘管它會「迷惑」環路條件:

while True: 
    sel = selection() 
    if sel == 0: # or perhaps "if not sel" 
     break 
    input() 
    if sel == 1: 
     ... 

快樂編碼。