我如何让我的程序转移到python中的下一个elif

Edw*_*ere -1 python if-statement

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'
Run Code Online (Sandbox Code Playgroud)

当我输入我对程序的猜测它只给我一个输出例如我输入8个程序输出"太高"但当我再次猜测输出是空白时,我该如何解决这个问题?你好,你叫什么名字?

 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
    >>> 
Run Code Online (Sandbox Code Playgroud)

mgi*_*son 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
Run Code Online (Sandbox Code Playgroud)

使用您的代码版本,您猜一次.如果你错了,你的程序会一遍又一遍地尝试相同的猜测(假设你break实际上应该缩进elif).您可能在终端中输入新的猜测,但您的程序从未看到它们.如果它break实际上在你的代码中的正确位置,那么你猜一次,无论是写还是错,它都会立即退出循环.

  • @Marcin - 我把猜测移到了while循环中.我也增加猜测的数量,并将休息时间放在正确的位置. (2认同)