2014-05-03 43 views
-1

我在永IDE運行蟒蛇2.75(CLOSED)賦值之前引用的局部變量'exit'?

代碼:

exit = False 

while not exit: 

    selection = int(raw_input("Press 1 to go and 0 to quit: ") 
    if selection == 1: 
     print("yay") 
    elif selection == 0: 
     print("Goodbye") 
     exit = True 
    else: 
     print("Go away") 

當我按下0,它說:

local variable 'exit' referenced before assignment 

什麼是錯的?

+0

OK,沒有他們arent對不起,錯字http://stackoverflow.com/questions/的 – user3595018

+0

重複9264763/unboundlocalerror合蟒。 – Veedrac

回答

2

你的代碼工作正常如下:

exit = False 

while not exit: 
    selection = int(raw_input("Press 1 to go and 0 to quit: "))  #added) to correct syntax error 
    if selection == 1: 
     print("yay") 
    elif selection == 0: 
     print("Goodbye") 
     exit = True 
    else: 
     print("Go away") 

DEMO

+0

祝賀你的10k代表狀態;) –

+0

你可以查看[this](http://stackoverflow.com/tools)!並刪除了答案;) –

+0

@Downvoter,你能解釋一下我的答案是否有問題?謝謝。 – sshashank124

0

你錯過了周圍的INT)密切括號(

不過,如果您使用的是while循環,爲什麼不簡單地使用休息而不是布爾?它更乾淨。而不是退出= True,只需鍵入break,循環將結束。

如果我對你輸入的內容的理解是正確的,你想結束了:

while True: 
     selection = int(raw_input("Press 1 to go and 0 to quit: ")) 
     if selection == 1: 
     print("yay") 
     elif selection == 0: 
     print("Goodbye") 
     break 
     else: 
     print("Go away") 
相關問題