Skip to content

Guess the Number Game

Abstract

This is a simple game where the user has to guess a number between 1 and 20. You will learn how to use a while loop with a break statement. In this application, computer will choose a random number between 1 and 20, and the user will have to guess the number. If the user guesses wrong, the program will tell the user if the guess is too high or too low. If the user guesses correctly, the program will congratulate the user and end the game. If the user fails to guess the number within six tries, the program will tell the user the number and end the game.

Prerequisites

  • Python 3.6 or above
  • Text Editor or IDE

Getting Started

Create a new project

  1. Create a new project folder and name it guessTheNumberGameguessTheNumberGame.
  2. Create a new file and name it guessTheNumberGame.pyguessTheNumberGame.py.
  3. Open the project folder in your favorite text editor or IDE.
  4. Copy the code below and paste it into your guessTheNumberGame.pyguessTheNumberGame.py file.

Write the code

  1. Add the following code to your guessTheNumberGame.pyguessTheNumberGame.py file.
⚙️ Guess The Number
Guess The Number
# Guess the number game
 
import random
 
print('Hello, what is your name?')
name = input()
 
print('Well, ' + name + ', I am thinking of a number between 1 and 20.')
secretNumber = random.randint(1, 20)
 
for guessesTaken in range(1, 7):
    print('Take a guess. You have ' + str(7 - guessesTaken) + ' guesses left.')
    guess = int(input())
 
    if guess < secretNumber:
        print('Your guess is too low.')
    elif guess > secretNumber:
        print('Your guess is too high.')
    else: 
        break # This condition is the correct guess!
    
if guess == secretNumber:
    print('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guesses.')
else:
    print('Nope. The number I was thinking of was ' + str(secretNumber) + '.') 
Guess The Number
# Guess the number game
 
import random
 
print('Hello, what is your name?')
name = input()
 
print('Well, ' + name + ', I am thinking of a number between 1 and 20.')
secretNumber = random.randint(1, 20)
 
for guessesTaken in range(1, 7):
    print('Take a guess. You have ' + str(7 - guessesTaken) + ' guesses left.')
    guess = int(input())
 
    if guess < secretNumber:
        print('Your guess is too low.')
    elif guess > secretNumber:
        print('Your guess is too high.')
    else: 
        break # This condition is the correct guess!
    
if guess == secretNumber:
    print('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guesses.')
else:
    print('Nope. The number I was thinking of was ' + str(secretNumber) + '.') 
  1. Save the file.
  2. Open the terminal and navigate to the project folder.
  3. Run the following command to run the application.
command
C:\Users\username\Documents\guessTheNumberGame> python guessthenumber.py
Hello, what is your name?
Ravi
Well, Ravi, I am thinking of a number between 1 and 20.
Take a guess. You have 6 guesses left.
23
Your guess is too high.
Take a guess. You have 5 guesses left.
12
Your guess is too high.
Take a guess. You have 4 guesses left.
11
Your guess is too high.
Take a guess. You have 3 guesses left.
7
Your guess is too high.
Take a guess. You have 2 guesses left.
5
Good job, Ravi! You guessed my number in 5 guesses.
command
C:\Users\username\Documents\guessTheNumberGame> python guessthenumber.py
Hello, what is your name?
Ravi
Well, Ravi, I am thinking of a number between 1 and 20.
Take a guess. You have 6 guesses left.
23
Your guess is too high.
Take a guess. You have 5 guesses left.
12
Your guess is too high.
Take a guess. You have 4 guesses left.
11
Your guess is too high.
Take a guess. You have 3 guesses left.
7
Your guess is too high.
Take a guess. You have 2 guesses left.
5
Good job, Ravi! You guessed my number in 5 guesses.

Explanation

  1. The import randomimport random statement will import the random module into your program. This will allow you to generate random numbers.
  2. The print('Hello, what is your name?')print('Hello, what is your name?') statement will print the string Hello, what is your name?Hello, what is your name? to the console.
  3. The name = input()name = input() statement will prompt the user to enter their name and store the value in the namename variable.
  4. The print('Well, ' + name + ', I am thinking of a number between 1 and 20.')print('Well, ' + name + ', I am thinking of a number between 1 and 20.') statement will print the string Well, Well, followed by the value of the namename variable, followed by the string , I am thinking of a number between 1 and 20., I am thinking of a number between 1 and 20. to the console.
  5. The secretNumber = random.randint(1, 20)secretNumber = random.randint(1, 20) statement will generate a random number between 1 and 20 and store the value in the secretNumbersecretNumber variable.
  6. The for guessesTaken in range(1, 7):for guessesTaken in range(1, 7): statement will create a for loop that will run six times. The guessesTakenguessesTaken variable will store the number of times the loop has run.
  7. The print('Take a guess. You have ' + str(7 - guessesTaken) + ' guesses left.')print('Take a guess. You have ' + str(7 - guessesTaken) + ' guesses left.') statement will print the string Take a guess. You have Take a guess. You have followed by the value of the guessesTakenguessesTaken variable subtracted from 7, followed by the string guesses left. guesses left. to the console.
  8. The guess = int(input())guess = int(input()) statement will prompt the user to enter a number and store the value in the guessguess variable.
  9. The if guess < secretNumber:if guess < secretNumber: statement will check if the value of the guessguess variable is less than the value of the secretNumbersecretNumber variable.
  10. The print('Your guess is too low.')print('Your guess is too low.') statement will print the string Your guess is too low.Your guess is too low. to the console.
  11. The elif guess > secretNumber:elif guess > secretNumber: statement will check if the value of the guessguess variable is greater than the value of the secretNumbersecretNumber variable.
  12. The print('Your guess is too high.')print('Your guess is too high.') statement will print the string Your guess is too high.Your guess is too high. to the console.
  13. The else:else: statement will run if the ifif and elifelif statements are false.
  14. The breakbreak statement will end the loop.
  15. The if guess == secretNumber:if guess == secretNumber: statement will check if the value of the guessguess variable is equal to the value of the secretNumbersecretNumber variable.
  16. The print('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guesses.')print('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guesses.') statement will print the string Good job, Good job, followed by the value of the namename variable, followed by the string ! You guessed my number in ! You guessed my number in , followed by the value of the guessesTakenguessesTaken variable, followed by the string guesses. guesses. to the console.

Next Steps

Congratulations! You have successfully created a Guess the Number game in Python. Experiment with the code and see if you can modify the application. Here are a few suggestions.

  • Change the number of guesses.
  • Change the range of numbers.
  • Add a high score.
  • Add a timer.
  • Add a GUI.
  • Add a database to store the high score.
  • Add a leaderboard.
  • Add a multiplayer mode.
  • Add a difficulty setting.

Conclusion

In this project, you learned how to create a Guess the Number game in Python. You also learned how to use a while loop with a break statement. You can find the source code Github

Was this page helpful?

Let us know how we did