繁体   English   中英

Python高位数字猜谜游戏

[英]Python higher lower number guessing game

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

我的问题是,当我运行该程序时,如果计算机询问我的数字是更高/更低/与数字相同(例如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!")

这里的问题是,为了使计算机程序知道您对某个值给出了错误的响应(例如,说“ 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 ,以至于很难阅读代码, 即使阅读代码的人已经知道它们代表变量名 。)

我在代码中运行了一堆打印程序,以检查执行时的变量。 这种行为

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所以第一个条件是False high50 ,我的答案是50所以第二个条件是False 但是, average46 ,不等于我的answer 50 Well played! 是最终结果。

elif answer > high:更改为elif answer >= high:您将获得预期的结果。 然后将elif answer < low:更改为elif answer <= low:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM