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
>>>
答案 0 :(得分: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
实际上位于代码中的正确位置,那么您可以猜测一次,无论是写还是错,它都会立即退出循环。
答案 1 :(得分:2)
你的休息时间超出你的if语量
它将执行while循环一次并且无论什么
都会中断