Pythondex
Python Game Programs

Guess Number Higher OR Lower Python Game

Last updated May 1, 2023 by Jarvis Silva

In this tutorial we will see how to create a guess number higher or lower python game, guess the number higher or lower game is like number guessing game where you have to guess a number between like 1 to 10 or 1 to 20 and If you guessed it in your first attempt then booyah you win.

If you guessed the number wrong the program will tell you either the number you guessed is higher or lower than the answer, you will have 3 attempts to guess the correct number.

It will be a fun and interesting game, you should definately create it if you are a beginner trying to learn python so let’s see how to create it.

Python Code For Guessing Number Higher OR Lower Game


from random import randint  # import the randint function from the random module

number = randint(1,10)  # generate a random integer between 1 and 10 and assign it to the variable "number"
attempts = 0  # initialize a counter for the number of attempts

print("Choose a random number from 1-10")

while attempts != 3:  # keep looping until the user has made 3 attempts
    guess = int(input("Make your guess: "))  # ask the user to enter their guess and convert it to an integer

    if guess > number:  # if the guess is too high
        print("Wrong, the number I guessed is lower than this, try again!")
        attempts = attempts + 1  # increment the number of attempts
    elif guess < number:  # if the guess is too low
        print("Wrong, the number I guessed is higher than this, try again!")
        attempts = attempts + 1  # increment the number of attempts
    else:  # if the guess is correct
        print("Booyah! You guessed correctly.")
        exit()  # end the program

print(f"The number is {number}")  # if the user has made 3 attempts without guessing correctly, print the correct number


Above is the complete python code for this higher or lower number guessing game, to run this program you need to have python installed on your computer, if you don’t have then follow this guide: Install and setup python on your computer or you can use this online python compiler.

Here is how this program works:

  • It imports the randint function from the random module.
  • It generates a random integer between 1 and 10 using the randint function and assigns it to the variable number.
  • It initializes the variable attempts to 0.
  • It prints a message asking the user to choose a random number between 1 and 10.
  • It enters a loop that runs as long as the variable attempts is not equal to 3.
  • Within the loop, it prompts the user to make a guess and reads their input as an integer.
  • It checks if the guess is greater than the random number generated in step 2. If so, it prints a message saying the number is lower and increments the attempts counter.
  • If the guess is less than the random number, it gives a clue saying the number is higher and increments the attempts counter.
  • If the guess is correct, it prints a message saying "Booyah! You guessed correctly." and exits the program.
  • If the loop finishes (i.e., the user has made 3 attempts), it prints a message telling the user what the random number was.

I have also added comments in the code about what each line does, below is an example output of this program.


Choose a random number from 1-10
Make your guess: 4
Wrong, the number I guessed is higher than this, try again!
Make your guess: 6
Wrong, the number I guessed is higher than this, try again!
Make your guess: 7
Booyah! You guessed correctly.

Choose a random number from 1-10
Make your guess: 7
Wrong, the number I guessed is higher than this, try again!
Make your guess: 8
Booyah! You guessed correctly.

Choose a random number from 1-10
Make your guess: 4
Wrong, the number I guessed is higher than this, try again!
Make your guess: 5
Booyah! You guessed correctly.

Choose a random number from 1-10
Make your guess: 1
Wrong, the number I guessed is higher than this, try again!
Make your guess: 3
Wrong, the number I guessed is higher than this, try again!
Make your guess: 4
Wrong, the number I guessed is higher than this, try again!
The number is 6

As you can we have successfully created the number guessing game in python, I hope you were able to follow along and understood how this program works.

Here are some more python games & programs for you:

I hope you found what you were looking for from this tutorial, do share it with your friends and if you want more tutorials like this then do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂