简体   繁体   English

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

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

In the process of learning Python using the book 'Python Programming for the Absolute Beginner Third Edition' and struggling with a challenge that has been set.在学习 Python 的过程中,使用《Python Programming for the Absolute Beginner Third Edition》一书,并与既定的挑战作斗争。

I have to create a Number Guessing program where the player picks a number and the program tries to guess it by picking a random number then using higher or lower questions to get closer to the number.我必须创建一个数字猜测程序,玩家选择一个数字,程序尝试通过选择一个随机数来猜测它,然后使用更高或更低的问题来接近这个数字。 I've got most of it figured out but I'm struggling with the higher or lower loop.我已经弄清楚了其中的大部分内容,但我正在为更高或更低的循环而苦苦挣扎。 The trouble I'm having is that i can't get the program to not go above or below it's second to last guess ie我遇到的麻烦是我无法让程序不超过或低于倒数第二个猜测,即

My number is 78 computer picks 50 i say higher computer picks 80 i say lower computer can then pick 12 (when i don't want it going below 50.我的数字是 78 电脑选择 50 我说更高的电脑选择 80 我说较低的电脑然后可以选择 12(当我不希望它低于 50 时。

I'm using Python 3.1我正在使用 Python 3.1

Here is a copy of the code.这是代码的副本。

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

Thanks in advance for any help you folks can give.在此先感谢您提供的任何帮助。

Ally盟友

I see the following problems with your code:我发现您的代码存在以下问题:

  1. your random number generators are bound only on one side of the number spectrum, eg random.randint(0, lower) - so it's ignoring the higher bound.您的随机数生成器仅绑定在数字谱higher一侧,例如random.randint(0, lower) - 因此它忽略了上限。 Similarly for computer_guess = random.randint(higher, 101) .同样对于computer_guess = random.randint(higher, 101)
  2. I suggest you initialise higher and lower .我建议你初始化higherlower
  3. Some debug helps:)一些调试帮助:)

Here's the working code:这是工作代码:

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

You are never storing the upper and lower bounds to the possible numbers.您永远不会存储可能数字的上限和下限。 In your example, as soon as your program picks 50 and you say "higher", you need to store somewhere the information that "the number is definitely higher than 50".在您的示例中,一旦您的程序选择 50 并且您说“更高”,您就需要在某处存储“该数字肯定高于 50”的信息。 The same goes for when you answer "lower".当您回答“较低”时也是如此。

You have to store something like min_comp_guess and max_comp_guess Then, when you are about to "guess" number you have to generate it for a valid range (obviously min_comp_guess up to max_comp_guess).你必须存储像 min_comp_guess 和 max_comp_guess 这样的东西然后,当你要“猜测”数字时,你必须为一个有效范围生成它(显然 min_comp_guess 到 max_comp_guess)。

I'm always second: :)我总是第二::)

I may be completely wrong but whilst testing your code I found that by choosing the programs number was higher than my chosen number it would increase the number instead or a presumable decrease, not making it any closer to the number I had guessed.我可能完全错了,但在测试您的代码时,我发现通过选择程序编号高于我选择的编号,它会增加编号或可能减少编号,而不是使它更接近我猜测的编号。 A similar higher/lower program I created had the same problem, to fix the problem I simply switched the more than and less than signs around, hopefully you find this helpful and could apply this to your program.我创建的一个类似的更高/更低的程序有同样的问题,为了解决这个问题,我简单地切换了大于和小于符号,希望你觉得这很有帮助并且可以将它应用到你的程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM