簡體   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