0

我编写了一些代码来确定 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))

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

4

3 回答 3

1

试试这个而不是那个条件。

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.')
于 2013-11-26T21:53:42.617 回答
0

条件不是那样工作的。你需要类似的东西:

# 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'):

并且由于land care truthy,该分支将始终执行。


您也可以考虑使用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.')
于 2013-11-26T21:52:42.743 回答
0

除了你的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))
于 2013-11-26T22:08:43.700 回答