Python-陷入猜谜游戏的程序?

时间:2013-10-24 10:58:54

标签: python

所以,我一直在研究这个猜谜游戏问题,过去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

3 个答案:

答案 0 :(得分: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?

答案 1 :(得分:0)

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

第二个问题是,当猜测太高时,您设置end = end - ans。虽然这种情况大部分时间都在使用二进制搜索并且总是猜到两半,但实际上并不是你想做的事情。如果您要在[start, end]范围内进行搜索,则应将end设置为仍可用于猜测的最高数字;当猜到太高时,这将是ans - 1

最后,您可能希望了解start == end的情况。在这种情况下,要么您已找到号码,要么用户输入了一些错误的内容。

另外作为一般提示,打印中间结果可以在调试时提供很多帮助。例如,您可以在循环顶部放置一个print(start, end),以查看每次猜测时您正在查看的范围。然后你会很容易注意到,范围的开始从未改变过。

答案 2 :(得分: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