2016-07-24 30 views
0

這裏沒有響應是我的代碼:Python的環路

from random import randint 
doorNum = randint(1, 3) 
doorInp = input("Please Enter A Door Number Between 1 and 3: ") 
x = 1 
while (x == 1) : 
    if(doorNum == doorInp) : 
     print("You opened the wrong door and died.") 
     exit() 
現在

,如果我碰巧得到了不吉利的數字工作正常。

else : 
    print("You entered a room.") 
    doorNum = randint(1, 3) 

這是完全停止響應的部分。我在bash交互式shell(Terminal,在osx上)運行它。它只是空白。

我是Python新手,我花了大部分時間作爲Web開發人員。

UPDATE:

感謝@rawing,我還不能給予好評(新手),這樣就會把它放在這裏。

+1

爲什麼'while(x == 1)'循環? –

+0

這是python2還是python3? –

+0

@Rawing python3我想。 – Ember

回答

-1

在python3中,input函數返回一個字符串。您將此字符串的值與隨機的int值進行比較。這將始終評估爲False。由於您只要求用戶輸入一次,在循環之前,用戶永遠不會有機會選擇新的號碼,並且循環會不斷地將一個隨機數與一個字符串進行比較。


我不知道究竟你的代碼是應該做的,但你可能想要做這樣的事情:

from random import randint 

while True: 
    doorNum = randint(1, 3) 
    doorInp = int(input("Please Enter A Door Number Between 1 and 3: ")) 

    if(doorNum == doorInp) : 
     print("You opened the wrong door and died.") 
     break 

    print("You entered a room.") 

參見:Asking the user for input until they give a valid response

0

如果您正在使用python3,然後input返回一個字符串,並將一個字符串與一個int進行比較總是爲false,因此您的exit()函數永遠不能運行。

0

您的doorInp變量是一個字符串類型,這是因爲您將它與if語句中的整數進行比較而引發該問題。您可以通過在輸入行後添加諸如print(type(doorInp))之類的內容來輕鬆進行檢查。 要修復它,只需將輸入語句括在int()中:doorInp = int(input("...."))