Python:简单对分搜索游戏中意外的条件激活

Python:简单对分搜索游戏中意外的条件激活,python,bisection,Python,Bisection,我写了一些代码来确定0到100之间的秘密号码。用户告诉机器猜测的数字(范围的一半)要么太高,要么太低,要么正好。根据输入,机器使用对分搜索来调整猜测。当猜测正确时,用户按c键,游戏结束。问题是,尽管在“我不理解输入”分支中设置了条件,但当用户按下c(有效条目)时,该分支会触发,而这不是第一次猜测 例如,以下是输出- Please think of a number between 0 and 100! Is your secret number 50? Enter 'h' to indicate

我写了一些代码来确定0到100之间的秘密号码。用户告诉机器猜测的数字(范围的一半)要么太高,要么太低,要么正好。根据输入,机器使用对分搜索来调整猜测。当猜测正确时,用户按c键,游戏结束。问题是,尽管在“我不理解输入”分支中设置了条件,但当用户按下c(有效条目)时,该分支会触发,而这不是第一次猜测

例如,以下是输出-

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. c
Sorry, I did not understand your input.
Game over. Your secret number was:75
>>> 
    High=100
    Low=0
    Guess=50
    user_input=0

    print('Please think of a number between 0 and 100!')

    while user_input != 'c':
        print("Is your secret number"+" "+str(Guess)+"?")
        userinput = 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_input == 'h':
            High=Guess
            Guess= ((High+Low)/2)
        if user_input == 'l':
            Low=Guess
            Guess= ((High+Low)/2)
        if user_input != 'h' or 'l' or 'c':
            print('Sorry, I did not understand your input.')

    print ('Game over. Your secret number was:'''+ str(Guess))
下面是代码-

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. c
Sorry, I did not understand your input.
Game over. Your secret number was:75
>>> 
    High=100
    Low=0
    Guess=50
    user_input=0

    print('Please think of a number between 0 and 100!')

    while user_input != 'c':
        print("Is your secret number"+" "+str(Guess)+"?")
        userinput = 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_input == 'h':
            High=Guess
            Guess= ((High+Low)/2)
        if user_input == 'l':
            Low=Guess
            Guess= ((High+Low)/2)
        if user_input != 'h' or 'l' or 'c':
            print('Sorry, I did not understand your input.')

    print ('Game over. Your secret number was:'''+ str(Guess))

提前谢谢。我已经为此绞尽脑汁好几个小时了……

条件句不是那样的。你需要像这样的东西:

# Check each condition explicitly
if user_input != 'h' and user_input != 'l' and user_input != 'c':
或:

您当前的方法被理解为

if (user_input != 'h') or ('l') or ('c'):
由于
l
c
是,所以该分支将始终执行


您也可以考虑使用<代码> ELIF < /C> >,因此您的条件将变成以下:

while True:
    if user_input == 'h':
        High=Guess
        Guess= ((High-Low)/2)
    elif user_input == 'l':
        Low=Guess
        Guess= ((High-Low)/2)
    elif user_input == "c":
        # We're done guessing. Awesome.
        break
    else:
        print('Sorry, I did not understand your input.')

用这个代替那个条件

if user_input not in ['h','l','c']:
      print('Sorry, I did not understand your input.')
您可能不必检查
user\u input
是否为
h
l
,因为前两个
if
应该处理这个问题

    if user_input == 'h':
        High=Guess
        Guess= ((High-Low)/2)
    elif user_input == 'l':
        Low=Guess
        Guess= ((High-Low)/2)
    elif user_input == 'c':
        pass # the while statement will deal with it or you could break
    else:
        print('Sorry, I did not understand your input.')

除了您的
if
,您的逻辑还有一些错误。我推荐如下:

High = 100
Low = 1
LastGuess = None

print('Please think of a number between 0 and 100!')

while True:
    Guess = int((High+Low)/2)
    if Guess == LastGuess:
        break
    print("Is your secret number"+" "+str(Guess)+"?")
    user_input = 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_input == 'h':
        High = Guess
        LastGuess = Guess
    elif user_input == 'l':
        Low = Guess
        LastGuess = Guess
    elif user_input == 'c':
        break
    else:
        print('Sorry, I did not understand your input.')

print ('Game over. Your secret number was:'''+ str(Guess))

@uselpa哎哟,修好了。谢谢你的反馈。你能指出其他逻辑错误吗?猜测应该是(高+低)/2。而且,当程序不断猜测相同的数字时,您不会停止。此外,没有理由在多个位置计算Guess。您似乎使用Python 2,因此需要使用
raw\u input()
,如示例所示。我使用了Python 3,这就是为什么我只使用
input()
,但这也需要使用
int()
来确保Guess是一个整数。好的,谢谢。我无意中用high-low粘贴了一个旧版本的代码,但是在多个地方的计算猜测是一个很好的观点,谢谢。谢谢你提供了关于条件的信息。我放弃了这个列表,因为我们在课堂上还没有达到他们的水平,我把猜测的计算加入了猜测本身。