-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 个程序输出“太高”但是当我再次猜测输出为空白时,我该如何解决这个问题?你好,你叫什么名字?

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

2 回答 2

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实际上在您的代码中的正确位置,那么您猜测一次,无论写入还是错误,它都会立即退出循环。

于 2012-08-02T18:33:39.433 回答
2

你的休息在你的 if 语句之外

它将执行一次while循环并无论如何都会中断

于 2012-08-02T18:34:06.083 回答