Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:26:56+00:00 2026-06-15T10:26:56+00:00

In the process of learning Python using the book ‘Python Programming for the Absolute

  • 0

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.

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 i.e.

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.

I’m using 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:26:57+00:00Added an answer on June 15, 2026 at 10:26 am

    I see the following problems with your code:

    1. your random number generators are bound only on one side of the number spectrum, e.g. random.randint(0, lower) – so it’s ignoring the higher bound. Similarly for computer_guess = random.randint(higher, 101).
    2. I suggest you initialise higher and lower.
    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")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm in the process of learning C++. But there's so much more that I
I'm using PyWin32's win32process.CreateProcess to start up a GUI program that has functionality I
I'm trying to pick up Python. As part of the learning process I'm porting
I am in the process of learning Python and had a question about the
I'm in the process of learning Python while implementing build scripts and such. And
I am learning Python and am reading through an example script that includes some
A little background: I'm in the process of learning Python through O'Reilly's, Learning Python
In the process of learning Django and Python. I can't make sense of this.
In the process of learning Ajax requests using jQuery, I tried to load google
I am just beginning to start learning web application development, using python. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.