繁体   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