Python 3.1数字猜测游戏更高或更低的循环

时间:2012-11-30 10:42:55

标签: python python-3.x

在使用“绝对初学者第三版的Python编程”一书学习Python的过程中,并努力应对已经设定的挑战。

我必须创建一个数字猜测程序,其中玩家选择一个数字,程序试图通过选择一个随机数然后使用更高或更低的问题来猜测它来接近数字。我已经弄明白了,但我正在努力应对更高或更低的循环。我遇到的麻烦是,我不能让程序不高于或低于它的倒数第二个猜测,即

我的号码是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")

提前感谢您的帮助。

同盟

4 个答案:

答案 0 :(得分:2)

我发现您的代码存在以下问题:

  1. 您的随机数生成器仅限于数字谱的一侧,例如random.randint(0, lower) - 所以它忽略了higher界限。同样适用于computer_guess = random.randint(higher, 101)
  2. 我建议您初始化higherlower
  3. 一些调试有助于:)
  4. 这是工作代码:

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

答案 1 :(得分:1)

您永远不会将上限和下限存储到可能的数字中。在您的示例中,只要您的程序选择50并且您说“更高”,您就需要存储“数字绝对高于50”的信息。当你回答“较低”时也是如此。

答案 2 :(得分:1)

你必须存储像min_comp_guess和max_comp_guess这样的东西 然后,当您要“猜测”数字时,您必须为有效范围生成它(显然min_comp_guess最多为max_comp_guess)。

我总是第二名! :)

答案 3 :(得分:0)

我可能完全错了,但在测试你的代码时,我发现通过选择程序编号高于我选择的编号,它会增加数字而不是可能的减少,而不是使它更接近我猜测的数字。我创建的类似的更高/更低的程序也遇到了同样的问题,为了解决这个问题,我只是简单地切换了周围的符号,希望你觉得这很有用,并且可以将它应用到你的程序中。