简体   繁体   English

我如何使我的程序进入python中的下一个elif

[英]How do i make my program move on to the next elif in 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'

when i enter my guess into the program it only gives me one output for example i enter 8 programs output is "too high" but when i guess again the output is blank, how do i fix this? 当我在程序中输入我的猜测时,例如,我只能输入一个输出,例如,我输入8个程序输出为“太高”,但是当我再次猜测输出为空白时,如何解决此问题? hello what is your name? 你好,你叫什么名字?

 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
    >>> 

I think this is what you want: 我认为这是您想要的:

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

With your version of the code, you guess once. 使用您的代码版本,您只需猜测一次。 If you're wrong, your program tries the same guess over and over again forever (assuming your break was actually supposed to be indented in the elif ). 如果您错了,您的程序将一遍又一遍地尝试相同的猜测(假设您的break实际上应该在elif缩进)。 You might be typing new guesses into the terminal, but your program never sees them. 您可能正在终端中输入新的猜测,但是您的程序永远看不到它们。 If the break was actually in the correct place in your code, then you guess once and whether write or wrong it exits the loop right away. 如果该break实际上在代码中的正确位置,那么您只需猜测一次,无论是写入还是错误,它都会立即退出循环。

your break is outside of your ifstatement 您的休息不在您的ifstatement中

It will execute while loop one time and break no matter what 它会在while循环中执行一次,无论发生什么都会中断

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM