【问题标题】:Python higher lower number guessing gamePython 高低数猜谜游戏
【发布时间】:2019-03-19 16:48:19
【问题描述】:

在这个程序中,你想到数字,计算机猜测。在游戏开始之前,计算机会询问它猜到了多少。如果计算机输了,它会询问正确答案是什么。它还会检查以确保答案是合法的,如果不合法,就会指出它。如果用户给出的答案不一致,计算机会指出并停止播放。

我的问题是,当我运行程序时,如果我说“更高”或“更低”,当计算机询问我的数字是否更高/更低/与某个数字相同时,比如 50,我稍后可以说我的数字是50 它告诉我我赢了,而不是说“那不可能;你说它高于/低于 50!”我该如何解决这个问题?

print("Think of a number between 1 and 100 and I'll guess it.")
total = int(input("How many guesses do I get? "))

h = "higher"
l = "lower"
s = "same"
low = 0
high = 100
hls = ""

guess_count = 0

average = 0

while guess_count < total and hls != s:
    average = (high + low) // 2
    hls = input("Is the number higher, lower, or the same as " + str(average) + "? ")
    if hls == h:
        low = average
    elif hls == l:
        high = average
    guess_count += 1
    if high - low == 1:
        break
if high - low == 1:
    print("Wait; how can it be both higher than " + str(low) + " and lower than " + str(high) + "?")
elif hls == s:
    print("I won!")
else:
    answer = int(input("I lost; what was the answer? "))
    if answer < low:
        print("That can't be; you said it was higher than " + str(low) + "!")
    elif answer > high:
        print("That can't be; you said it was lower than " + str(high) + "!")
    elif answer != average:
        print("Well played!")

【问题讨论】:

  • 这类问题应该在Code Review上。反正我没看懂这个问题:如果你输入50,你应该在第一个问题回答same,程序获胜。附带说明一下,如果用户在猜测阶段提供了意外输入,您仍然将 1 添加到 guess_count

标签: python if-statement while-loop


【解决方案1】:

我运行了您的代码,其中包含一堆打印件,以检查其执行时的变量。这种行为

Think of a number between 1 and 100 and I'll guess it.
How many guesses do I get? 5
Is the number higher, lower, or the same as 50? lower
High is: 50, low is: 0, average is: 50
Is the number higher, lower, or the same as 25? higher
High is: 50, low is: 25, average is: 25
Is the number higher, lower, or the same as 37? higher
High is: 50, low is: 37, average is: 37
Is the number higher, lower, or the same as 43? higher
High is: 50, low is: 43, average is: 43
Is the number higher, lower, or the same as 46? higher
High is: 50, low is: 46, average is: 46
I lost; what was the answer? 50
High is: 50, low is: 46, average is: 46
Well played!

所以,当计算机丢失时,average46,它会通过这些条件:

if answer < low:
    print("That can't be; you said it was higher than " + str(low) + "!")
elif answer > high:
    print("That can't be; you said it was lower than " + str(high) + "!")
elif answer != average:
    print("Well played!")

low46,答案是50,所以第一个条件是Falsehigh50 而我的答案是 50 所以第二个条件是 False。但是,average46,不等于我的 answer50Well played! 是最终结果。

elif answer &gt; high: 更改为elif answer &gt;= high:,您将获得预期的结果。然后把elif answer &lt; low:改成elif answer &lt;= low:

【讨论】:

    【解决方案2】:

    这里的问题是,为了让计算机程序知道您对某个值给出了错误的响应(例如,说“50 太低”而实际上答案是 50),它需要记录它的猜测以及你对这些猜测的反应。

    因此,在它进行猜测并且您获得“较低”或“较高”响应后,您可以将猜测放入 low_guesseshigh_guesses 列表中,然后在游戏结束时检查这些列表。你可以有这样的东西:

    low_guesses = []
    high_guesses = []
    
    while True:  # breaks out when user types "same"
        response = input("Is the number higher, lower, or the same as " + str(guess) + "? ")
        if response == "lower":
            # Add the guess to the low_guesses array:
            low_guesses.append(guess)
        elif response == "higher":
            # Add the guess to the high_guesses array:
            high_guesses.append(guess)
        else:
            # Secret number found, so break out of loop:
            break
    
    # Outside of the loop, examine your low_guesses and high_guesses lists.
    

    您可以检查列表,确保low_guesses 的所有元素都小于密码,并且high_guesses 的所有元素都大于密码。如果不是这样,你就知道有问题了。

    (另外,一些建议:请不要将变量命名为llst。它们看起来非常像11st,这使得阅读您的代码更难阅读,即使在阅读您的代码的人已经知道它们代表变量名。)

    【讨论】:

    • 感谢您的建议!我一定会更加注意赋予变量更清晰的名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    相关资源
    最近更新 更多