如何让我的程序移动到下一个ELIF在python

问题描述:

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

我想这是你想要什么:

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

您的假期是你ifstatement

之外它将执行while循环一次,打破不管什么