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

2024-04-26 02:45:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一些代码来确定一个0到100之间的秘密号码。用户告诉机器猜测的数字(范围的一半)要么太高,要么太低,要么刚好。根据输入,机器使用对分搜索来调整猜测。当猜测正确时,用户按c键游戏结束。问题是,尽管“i didn't understand input”分支中存在条件,但当用户按下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
>>> 

这里的代码是strong

    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))

提前谢谢。我已经为此绞尽脑汁好几个小时了。。。。你知道吗


Tags: thetonumberinputyoursecretislow
3条回答

试试这个而不是那个条件。你知道吗

if user_input not in ['h','l','c']:
      print('Sorry, I did not understand your input.')

您可能不必检查user_inputh还是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.')

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

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

或:

# Check if the input is one of the elements in the given list
if user_input not in ["h", "c", "l"]:

你目前的做法被理解为

if (user_input != 'h') or ('l') or ('c'):

由于lctruthy,所以该分支将始终执行。你知道吗


您还可以考虑使用elif,因此您的条件如下:

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之外,您的逻辑还有一些错误。我推荐这样的:

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))

相关问题 更多 >