Python- 卡在猜谜游戏的程序上?

标签 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

最佳答案

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

关于Python- 卡在猜谜游戏的程序上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19564050/

相关文章:

python - Discord.py:有没有办法使用 on_message() 函数获取命令的参数?

python - 在 python 中同时产生两个变量时出错

python - 如何在 Django 中使用 Ajax 提交表单

python - 如何判断函数来自哪个模块?

python - Chameleon 模板全局变量

python - 当且仅当 TABLE 不存在时,我如何创建它?

python:如何直接从字节字符串变量显示图像?

python - JSON数据转换为django模型

python - 如何访问列表字典中的每个元素?

python - 类型错误 : <unknown> is not a numpy array in cv2. calcOpticalFlowPyrLK()