简体   繁体   English

Python高位数字猜谜游戏

[英]Python higher lower number guessing game

In this program, you think of the number, the computer guesses. 在这个程序中,计算机会猜测数字。 Before the game begins, the computer asks how many guesses it gets. 在游戏开始之前,计算机会询问获得了多少猜测。 If the computer loses, it asks what the correct answer was. 如果计算机丢失,它将询问正确的答案是什么。 It also checks to make sure that the answer is legal, and points it out if it wasn't. 它还会检查以确保答案是合法的,并指出答案是否正确。 If the user gives inconsistent answers, the computer points it out and stop playing. 如果用户给出的答案不一致,则计算机会指出并停止播放。

My issue is that when I run the program, if I say "higher" or "lower" when the computer asks if my number is higher/lower/same as a number, say 50, I can later say my number was 50 and it tells me that I win instead of saying "That can't be; you said it was higher/lower than 50!" 我的问题是,当我运行该程序时,如果计算机询问我的数字是更高/更低/与数字相同(例如50)时是说“更高”还是“更低”,我以后可以说我的数字是50告诉我我赢了,而不是说“那不可能;您说的是高于/低于50!” How would I fix this problem? 我该如何解决这个问题?

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

The issue here is that in order for the computer program to know that you gave the wrong response for a value (like, say, saying that "50 is too low" when in fact the answer is 50), it needs to have a record of its guesses and your responses to those guesses. 这里的问题是,为了使计算机程序知道您对某个值给出了错误的响应(例如,说“ 50太低”,而实际上答案是50),它需要有一条记录它的猜测以及您对这些猜测的回应。

So after it makes a guess and you obtain a "lower" or "higher" response, you could place the guess in a low_guesses or high_guesses list, and then examine those list at the end of the game. 因此,在进行猜测并获得“较低”或“较高”的响应之后,可以将猜测放入low_guesseshigh_guesses列表中,然后在游戏结束时检查这些列表。 You could have something like this: 您可能会有这样的事情:

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.

You can examine the lists by making sure that all the elements of low_guesses are less than the secret number, and that all the elements of high_guesses are greater than the secret number. 您可以通过确保low_guesses所有元素都小于秘密数字,并且high_guesses所有元素都大于秘密数字来检查列表。 If that's not true, you know that something is wrong. 如果那不是真的,则说明您有问题。

(Also, some advice: Please don't name variables l or lst . They look so much like 1 or 1st that it makes reading your code harder to read, even when those reading your code already know that they represent variable names .) (另外,一些建议:请不要命名变量llst 。它们看起来很像11st ,以至于很难阅读代码, 即使阅读代码的人已经知道它们代表变量名 。)

I ran your code with a bunch of prints in it to check the variables as it executes. 我在代码中运行了一堆打印程序,以检查执行时的变量。 This behavior 这种行为

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!

So, when the computer lost, and the average was 46 , it ran through these conditionals: 因此,当计算机丢失时, 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!")

low was 46 , and answer was 50 so the first condition is False . low46 ,答案是50所以第一个条件是False high was 50 and my answer was 50 so the second condition is False . high50 ,我的答案是50所以第二个条件是False However, average is 46 , and not equal to my answer which is 50 . 但是, average46 ,不等于我的answer 50 Well played! is the end result. 是最终结果。

Change elif answer > high: to elif answer >= high: and you'll get the expected result. elif answer > high:更改为elif answer >= high:您将获得预期的结果。 Then change elif answer < low: to elif answer <= low: . 然后将elif answer < low:更改为elif answer <= low:

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

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