2012-08-02 29 views
-1
import random 
print"hello what is your name?" 
name = raw_input() 
print"hello", name 
print"wanna play a game? y, n" 
choice = raw_input() 
if choice =='y': 
    print'good lets start a number guessing game' 

elif choice =='n': 
    print'maybe next time' 
    exit() 

random.randint(1,10) 
number = random.randint(1,10) 
print'pick a number between 1-10' 
numberofguesses = 0 
guess = input() 

while numberofguesses < 10: 
if guess < number: 
    print"too low" 
elif guess > number: 
     print"too high" 
elif guess == number: 
     print'your correct the number is', number 
break 
if guess == number: 
    print'CONGRATS YOU WIN THE GAME' 

當我進入我的猜測到程序中只給了我例如 一個輸出我進入8 程序輸出是「過高」 但當我再次猜測輸出是空白時,我該如何解決這個問題? 你好你叫什麼名字?如何讓我的程序移動到下一個ELIF在python

ed 
    hello ed 
    wanna play a game? y, n 
    y 
    good lets start a number guessing game 
    pick a number between 1-10 
    2 
    too low 
    >>> 5 
    5 
    >>> 3 
    3 
    >>> 2 
    2 
    >>> 
+0

顯示您的輸入和輸出。你對你的問題的描述是不可思議的。 http://sscce.org/ – Marcin 2012-08-02 18:31:25

+1

@EdwardLaPiere:那些'>>>'你看到的意思是你已經不在你的程序中了。這些數字正在由口譯員進行評估,只是被吐了回來。 (你的程序因爲你的'break'在錯誤的地方而終止,如下面的評論所述)。 – gsk 2012-08-02 18:35:42

+0

這是更好嗎? – 2012-08-02 18:36:49

回答

5

我想這是你想要什麼:

numberofguesses = 0 

while numberofguesses < 10: 
guess = input() #int(raw_input("Pick a number between 1 and 10: ")) would be much better here. 
numberofguesses+=1 
if guess < number: 
    print "too low" 
elif guess > number: 
    print "too high" 
elif guess == number: 
    print 'your correct the number is', number 
    break 

與您的代碼版本,你猜一次。如果你錯了,你的程序會一遍又一遍地嘗試相同的猜測(假設你的break實際上應該在elif中縮進)。您可能會在終端中輸入新的猜測,但您的程序從未看到它們。如果break實際上位於代碼中的正確位置,那麼您只需猜測一次,並立即寫入或錯誤地退出循環。

+0

這與OP的代碼有什麼不同? – Marcin 2012-08-02 18:34:06

+0

休息是在正確的地方... – 2012-08-02 18:34:23

+2

@Marcin - 我把猜測轉移到while循環。我也增加了猜測的數量,並把這個休息放在正確的地方。 – mgilson 2012-08-02 18:35:05

2

您的假期是你ifstatement

之外它將執行while循環一次,打破不管什麼

相關問題