-1

在使用“绝对初学者第三版的 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

4 回答 4

2

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

  1. 您的随机数生成器仅绑定在数字谱的一侧,例如random.randint(0, lower)- 所以它忽略了higher边界。同样对于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")
于 2012-11-30T11:09:19.210 回答
1

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

于 2012-11-30T10:49:38.443 回答
1

您必须存储诸如 min_comp_guess 和 max_comp_guess 之类的内容。然后,当您要“猜测”数字时,您必须为有效范围生成它(显然 min_comp_guess 到 max_comp_guess)。

我永远是第二!:)

于 2012-11-30T10:52:11.100 回答
0

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

于 2015-03-03T22:01:42.080 回答