帮我调试一个Python猜数字程序
我正在尝试写一个Python程序,让程序来猜用户的数字。我觉得自己有点傻,卡在数学部分,感觉这就像是那种我已经努力了几个小时,但别人可能能轻松地从新角度看问题并解决它。我想先把用户的输入部分几乎完全去掉。以下是我目前的进展,程序在逻辑部分卡住了,最后只会不停地说猜的数字要么太低,要么太高,永远这样下去:
MIN=0
MAX=100
firstguess = MAX - MIN
firstguess = firstguess/2
while 1==1:
number = int(raw_input("Enter your number 0-100:"))
print firstguess
oldguess = firstguess
if firstguess > number:
print "First guess is too high."
raw_input()
guess = int(25)
print guess
while guess != number:
if guess > number:
print "My guess was too high."
raw_input()
nextguess = oldguess - guess
nextguess = nextguess/2
nextguess = guess - nextguess
oldguess = guess
guess = nextguess
elif guess == number:
print "I win!"
exit
elif guess < number:
print "My guess was too low."
raw_input()
nextguess = oldguess - guess
nextguess = nextguess/2
nextguess = nextguess + guess
oldguess = guess
guess = nextguess
elif firstguess == number:
print "I win!"
elif firstguess < number:
print "My first guess was too low."
raw_input()
guess = 75
print guess
print guess
while guess != number:
if guess > number:
print "My guess was too high."
raw_input()
nextguess = oldguess - guess
nextguess = nextguess/2
nextguess = guess - nextguess
oldguess = guess
guess = nextguess
elif guess == number:
print "I win!"
exit
elif guess < number:
print "My guess was too low."
raw_input()
nextguess = oldguess - guess
nextguess = nextguess/2
nextguess = nextguess + guess
oldguess = guess
guess = nextguess
2 个回答
2
好的,首先我不懂Python,其次我也不知道你的代码在干嘛,懒得去弄明白。为什么呢?因为我觉得你把事情搞得太复杂了。
我认为你可以用一个简单的二分查找来解决这个问题。用伪代码表示就是:
low = 0 // lowest possible value of user's number
high = 100 // highest possible value of user's number
while low < high
guess = (low + high) / 2 // guess the average of low and hight
if guess is too low // if guess is too low
then low = guess + 1 // then the lowest possible value is guess + 1
if guess it too high // if guess is too high
then hight = guess - 1 // then the highest possible value is guess - 1
if guess equals the value
we've found the answer
at this point (low + high) / 2 should be the answer
这个算法会把搜索的范围一分为二,然后检查用户的数字在哪一边。举个例子,在范围 1-10
中,假设用户的数字是 3
。这里 L
代表低值,H
代表高值,G
代表猜测:
L G H | guess is too high so we set high to guess-1
0 1 2 3 4 5 6 7 8 9 10 |
|
L G H | guess is too low so we set low to guess+1
0 1 2 3 4 5 6 7 8 9 10 |
|
L H | guess is in the same position as low and is correct so
0 1 2 3 4 5 6 7 8 9 10 | we've found the number
1
我可能在帮你做作业,但你看起来确实对这个问题做了认真尝试:
bounds = [0, 100]
minimum, maximum = bounds
number = int(raw_input('Pick a number [{0}-{1}]: '.format(minimum, maximum)))
while guess != number:
guess = int((maximum + minimum) / 2)
raw_input('Guessing {0}'.format(guess))
if guess > number:
maximum = guess
elif guess < number:
maximum = bounds[1]
minimum = guess
print 'I win!'
这个逻辑很简单:
- 先设定一个猜测的范围,包括最小值和最大值。
- 让用户选择一个数字,存储为
number
。 - 只要猜测的数字不等于用户选择的数字,就一直重复以下步骤:
- 找出最小值和最大值之间的中间值。
- 如果猜测的数字比用户选择的数字大,那说明用户选择的数字一定比这个猜测小,所以我们把最大猜测值设为当前的猜测值。
- 如果猜测的数字比用户选择的数字小,那我们就把最大值设为可能的最大数字,把最小值设为当前的猜测值。
- 一旦猜测的数字等于用户选择的数字,循环就结束,我们就赢了。