2015-10-04 28 views
-2
import random 

winning_conditon = 0 
no_of_guesses = 0 
comp_guess = random.randint(1,100) 

while (no_of_guesses == 11) or (winning_conditon == 1): 
    user_guess = int(input("What is your guess? ")) 
    if user_guess < 1 or user_guess > 100: 
     print("Your number is invalid!") 

    elif comp_guess == user_guess: 
     print("Well Done!") 
     winning_condition = 1 
    elif comp_guess < user_guess: 
     print("Your number is too high!") 
    elif comp_guess > user_guess: 
     print("Your number is too low!") 

    no_of_guesses = no_of_guesses + 1 
    print(no_of_guesses) 
print("You haven't guessed the right amount of times or u won.") 

每當我啓動Python IDLE(我使用的是可移植的Python 3.2.5.1(http://portablepython.com/wiki/PortablePython3.2.5.1/))它會產生一個重新啓動消息,然後它會顯示一個「=」號並且不會繼續該程序。你知道一個解決辦法嗎?當我在IDLE環境下啓動程序時,如何解決Python重新啓動問題?

+0

這是否與您發佈的代碼有關? – jonrsharpe

+0

刪除初始縮進後,程序運行。從IDLE編輯器運行時,RESTART行是正常的。這意味着程序運行在一個新的命名空間中,就像從命令行運行一樣。由於while條件爲false,所以循環從不運行。您需要用戶的答案中的「不」。最後一行被打印,程序退出。我在打印的行後看到的是>>> >>>,也就像用戶的回答一樣。如果您在控制檯中運行'python -i ',則會看到相同的輸出。 –

回答

0

當我運行您的程序,我得到這樣的輸出:

Python 3.2.5 (default, May 15 2013, 23:06:03) [MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> ================================ RESTART ================================ 
>>> 
You haven't guessed the right amount of times or u won. 
>>> 

這是完全正常和預期。

我會修改你的程序是這樣的:

while not (no_of_guesses == 11 or winning_conditon == 1) 

while not將是until相當。

相關問題