簡體   English   中英

Python 3.1 數字猜謎游戲更高或更低的循環

[英]Python 3.1 number guessing game higher or lower loop

在學習 Python 的過程中,使用《Python Programming for the Absolute Beginner Third Edition》一書,並與既定的挑戰作斗爭。

我必須創建一個數字猜測程序,玩家選擇一個數字,程序嘗試通過選擇一個隨機數來猜測它,然后使用更高或更低的問題來接近這個數字。 我已經弄清楚了其中的大部分內容,但我正在為更高或更低的循環而苦苦掙扎。 我遇到的麻煩是我無法讓程序不超過或低於倒數第二個猜測,即

我的數字是 78 電腦選擇 50 我說更高的電腦選擇 80 我說較低的電腦然后可以選擇 12(當我不希望它低於 50 時。

我正在使用 Python 3.1

這是代碼的副本。

import random

computer_tries = 0
player_number = None
computer_guess = random.randint(1, 100)

print(
    """
    Welcome Player to the fabulous number guessing game.
    Please allow me to show you my incredible deduction skills
    """)

question = None
while question != ("yes"):
    question = input("Has the player picked a number? ")
    question = question.lower()
    if question == "yes":
        print("\nI will now guess your number!!!\n")
        while player_number == None:
            computer_tries += 1
            print(computer_guess, "\n")
            confirmation = input("Is this the correct number? ")
            confirmation = confirmation.lower()
            if confirmation == "yes":
                player_number = computer_guess
                if computer_tries < 2:
                    print("I did it! I guessed your number was", computer_guess,
                           "and it only \ntook me", computer_tries,
                           "try to get it right!")
                else:
                    print("I did it! I guessed your number was", computer_guess,
                           "and it only \ntook me", computer_tries,
                           "tries to get it right!")
            else:
                higher_lower = None
                while higher_lower not in ("higher", "lower"):
                    higher_lower = input("Is my guess higher or lower"
                                + " than your number? ")
                    higher_lower = higher_lower.lower()
                    if higher_lower == "higher":
                        higher = computer_guess
                        computer_guess = random.randint(higher, 101)
                    elif higher_lower == "lower":
                        lower = computer_guess
                        computer_guess = random.randint(0, lower)
                    else:
                        print("Please choose either higher or lower.")



input("\n\nPress the enter key to exit")

在此先感謝您提供的任何幫助。

盟友

我發現您的代碼存在以下問題:

  1. 您的隨機數生成器僅綁定在數字譜higher一側,例如random.randint(0, lower) - 因此它忽略了上限。 同樣對於computer_guess = random.randint(higher, 101)
  2. 我建議你初始化higherlower
  3. 一些調試幫助:)

這是工作代碼:

import random

computer_tries = 0
player_number = None
computer_guess = random.randint(1, 100)

print(
    """
    Welcome Player to the fabulous number guessing game.
    Please allow me to show you my incredible deduction skills
    """)

question = None
lower = 0 # initial lower guess
higher = 101  # initial higher guess
while question != ("yes"):
    question = input("Has the player picked a number? ")
    question = question.lower()
    if question == "yes":
        print("\nI will now guess your number!!!\n")
        while player_number == None:
            computer_tries += 1
            print(computer_guess, "\n")
            confirmation = input("Is this the correct number? ")
            confirmation = confirmation.lower()
            if confirmation == "yes":
                player_number = computer_guess
                if computer_tries < 2:
                    print("I did it! I guessed your number was", computer_guess,
                           "and it only \ntook me", computer_tries,
                           "try to get it right!")
                else:
                    print("I did it! I guessed your number was", computer_guess,
                           "and it only \ntook me", computer_tries,
                           "tries to get it right!")
            else:
                higher_lower = None
                while higher_lower not in ("higher", "lower"):
                    higher_lower = input("Is my guess higher or lower"
                                + " than your number? ")
                    higher_lower = higher_lower.lower()
                    if higher_lower == "higher":
                        higher = computer_guess
                        computer_guess = random.randint(lower+1, higher-1)
                    elif higher_lower == "lower":
                        lower = computer_guess
                        computer_guess = random.randint(lower+1, higher-1)
                    else:
                        print("Please choose either higher or lower.")
                    print("DEBUG: number must be " + str(lower) + " < x < " + str(higher))


input("\n\nPress the enter key to exit")

您永遠不會存儲可能數字的上限和下限。 在您的示例中,一旦您的程序選擇 50 並且您說“更高”,您就需要在某處存儲“該數字肯定高於 50”的信息。 當您回答“較低”時也是如此。

你必須存儲像 min_comp_guess 和 max_comp_guess 這樣的東西然后,當你要“猜測”數字時,你必須為一個有效范圍生成它(顯然 min_comp_guess 到 max_comp_guess)。

我總是第二::)

我可能完全錯了,但在測試您的代碼時,我發現通過選擇程序編號高於我選擇的編號,它會增加編號或可能減少編號,而不是使它更接近我猜測的編號。 我創建的一個類似的更高/更低的程序有同樣的問題,為了解決這個問題,我簡單地切換了大於和小於符號,希望你覺得這很有幫助並且可以將它應用到你的程序中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM