Python 这里怎么了?

Python 这里怎么了?,python,python-3.6,bisection,Python,Python 3.6,Bisection,我正在尝试用python制作一个使用对分搜索的猜谜游戏。我的代码是这样的 print('please think of a number between 1 and 100') high=100 low=1 guess=int((high+low)/2) n=input('is your number 50? press y to yes, l to low, h to high ') while n!='y': if n=='h': high=guess

我正在尝试用python制作一个使用对分搜索的猜谜游戏。我的代码是这样的

print('please think of a number between 1 and 100')
high=100
low=1
guess=int((high+low)/2)
n=input('is your number 50? press y to yes, l to low, h to high ')
while n!='y':
    if n=='h':
        high=guess
        n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
    elif n=='l':
        low=guess
        n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
        print() 
    else:
        n=input('wrong input. press y to yes, l to low, h to high ')
        print()
    guess=int((high+low)/2)
print('game over. i guessed correctly')

但不幸的是,我没有从这段代码中得到想要的东西。你介意帮我吗?提前感谢

在while循环中向用户显示提示之前,您应该更新guess

while n!='y':
    if n=='h':
        high=guess
        guess=int((high+low)/2)
        n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
    elif n=='l':
        low=guess
        guess=int((high+low)/2)
        n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
        print() 
    else:
         n=input('wrong input. press y to yes, l to low, h to high ')
         print()

所以问题是,在更改高/低之后,但在将其打印给用户之前,您没有更新“猜测”

这是修改后的代码

注意,我还更改了消息的语言,以便用户更清楚地知道何时应键入“h”和何时应键入“l”


guess
的更新可能也有错误,我的意思是,当猜测值低于预期值时,您应该从当前猜测值猜测一个新值到上一个
high
,因此只需更改
low
的值:

if n=='h':
    low=guess
    guess=int((high+low)/2)

期望它是一个二进制搜索场景,我对优化做了一些修改

print('please think of a number between 1 and 100')
high=100
low=1
guess=int(low+(high-low)/2)
n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
while n!='y':
    if n=='h':
        low=guess
    elif n=='l':
        high=guess
    else:
        n=input('wrong input. press y to yes, l to low, h to high ')
    guess=int(low+(high-low)/2)
    n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
print('game over. i guessed correctly')

您期望得到什么以及结果是什么?您需要解释此代码中所需的内容以获得解决方案。有几件事:(1)在
输入
调用中提示之前更新
猜测
,否则您将显示以前的猜测,(2)在除法时使用
/
而不是
//code>,否则你最终会得到一个浮点猜测。那是一个愚蠢的错误。在这里,我试图解释我想要的东西。首先,我的程序会让用户想到一个数字。之后,它将询问此人的数字是否为(高+低)/2[需要注意的是,通过查看我的代码,您将分别了解高和低的值],并且它将继续这样更改高和低的值。在获得y作为输入后,程序将停止。希望大家都能理解你还有另一个bug:(或者可能不取决于“1到100”的含义。如果用户选择100,你的脚本将永远不会结束,因为
int(number)
的工作原理:基本上,如果选择100,你将在高=100,低=99,然后99+100/2==99.5的情况下结束。然后是int(99.5)==99。所以你陷入了一个循环。
print('please think of a number between 1 and 100')
high=100
low=1
guess=int(low+(high-low)/2)
n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
while n!='y':
    if n=='h':
        low=guess
    elif n=='l':
        high=guess
    else:
        n=input('wrong input. press y to yes, l to low, h to high ')
    guess=int(low+(high-low)/2)
    n=input('is your number '+str(guess)+'? press y to yes, l to low, h to high ')
print('game over. i guessed correctly')