2

所以,我一直在研究这个猜谜游戏问题,在过去的 2 个小时里,我一直在挠头,试图找出问题所在,但我做不到。我也尝试搜索解决方案,但我不想复制和粘贴,我实际上想自己解决我的代码。

这是我到目前为止所能得到的:

start = 0
end = 100
print 'Please think of a number between 0 and 100!'
user = ''
ans = (start + end) / 2

while user != 'c':
    print ('Is your secret number ' +  str((start + end) / 2) + '?')
    user = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
    if user == 'l':
        start = ans
    elif user == 'h':
        end = end - ans
    ans = start
print 'Game over. Your secret number was: ' + str((start + end) / 2)

我究竟做错了什么?编辑:游戏应该像这样运行:

Please think of a number between 0 and 100!
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 75?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 87?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 81?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 84?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 82?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 83?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. c
Game over. Your secret number was: 83
4

3 回答 3

1

你是设置ans = start,那是错误。既然你想自己解决这个问题,我就不多解释了。这就是导致您的程序永远不会低于 25 的原因:

Please think of a number between 0 and 100!
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 25?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 25?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 25?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 25?
于 2013-10-24T11:03:26.013 回答
0

我发现您当前的代码存在两个问题。首先,你永远不会ans在循环中改变。您可能希望ans包含当前猜测的数字。所以你应该在猜测时设置它并直接在输出中使用它。所以移动ans = (start + end) / 2循环开始处的行。

第二个问题,是你设置end = end - ans的时候猜测太高了。虽然这在大多数情况下都有效,因为您使用二进制搜索并且总是猜测一半,但这并不是您真正想要做的。如果要在该范围内搜索,[start, end]则应设置end为仍可猜测的最高数字;那是ans - 1你猜得太高的时候。

最后,您可能想了解start == end. 在这种情况下,要么你找到了号码,要么用户输入了一些错误的东西。

同样作为一般提示,打印出中间结果在调试时会有很大帮助。例如,您可以将 aprint(start, end)放在循环的顶部,以查看您在每次猜测期间查看的范围。然后你会很容易注意到,范围的开始从未改变。

于 2013-10-24T11:03:30.283 回答
0

我的解决方案有效:

import random
numH = 100
numL = 0
num = random.randint(numL, numH)
print num

x = raw_input('Enter high, low or number 1: ')
while x == 'l' or x == 'h':
    if x == 'l':
        numH = num - 1
        num = random.randint(numL, numH)
        print num
        x = raw_input('Enter high, low or number 2: ')
    if x == 'h':
        numL = num + 1
        num = random.randint(numL, numH)
        print num
        x = raw_input('Enter high, low or number 2: ')  
    else:
        print 'your number is ', num
于 2015-01-20T02:37:38.053 回答