Python 巨蟒-陷入猜谜游戏';什么节目?

Python 巨蟒-陷入猜谜游戏';什么节目?,python,Python,所以,我一直在研究这个猜谜游戏的问题有一段时间了,在过去的两个小时里,我一直在绞尽脑汁想到底出了什么问题,但我做不到。我也尝试过寻找解决方案,但我不想做复制粘贴,实际上我想自己解决我的代码 以下是迄今为止我所能得到的: start = 0 end = 100 print 'Please think of a number between 0 and 100!' user = '' ans = (start + end) / 2 while user != 'c': print ('Is

所以,我一直在研究这个猜谜游戏的问题有一段时间了,在过去的两个小时里,我一直在绞尽脑汁想到底出了什么问题,但我做不到。我也尝试过寻找解决方案,但我不想做复制粘贴,实际上我想自己解决我的代码

以下是迄今为止我所能得到的:

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

您正在设置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?

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

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

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

另外,作为一般提示,打印出中间结果在调试时会有很大帮助。例如,您可以在循环的顶部放置一个
打印(开始、结束)
,以查看您在每次猜测中看到的范围。那么您很容易就会注意到,范围的开始从未改变。

我的解决方案有效:

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

你对代码有什么问题?当你运行它时会发生什么?对于一些输入,您希望得到什么输出,以及您实际得到什么输出?另外,这似乎是python 2格式的,为什么不使用python 3?我正在学习关于CS和编程介绍的Edx课程,他们正在使用python 2.x,这就是原因。但对于我自己的项目,我总是使用Py 3.x.:)@GamesBrainiac这有关系吗?OP有一个几乎完整的解决方案的问题,所以请求帮助是完全可以的。@poke-Well如果每个人都作弊,荣誉证书的价值就会降低:P