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
- Create a new project folder and name it
guessTheNumberGame
guessTheNumberGame
. - Create a new file and name it
guessTheNumberGame.py
guessTheNumberGame.py
. - Open the project folder in your favorite text editor or IDE.
- Copy the code below and paste it into your
guessTheNumberGame.py
guessTheNumberGame.py
file.
Write the code
- Add the following code to your
guessTheNumberGame.py
guessTheNumberGame.py
file.
⚙️ 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 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) + '.')
- Save the file.
- Open the terminal and navigate to the project folder.
- Run the following command to run the application.
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.
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
- The
import random
import random
statement will import the random module into your program. This will allow you to generate random numbers. - The
print('Hello, what is your name?')
print('Hello, what is your name?')
statement will print the stringHello, what is your name?
Hello, what is your name?
to the console. - The
name = input()
name = input()
statement will prompt the user to enter their name and store the value in thename
name
variable. - 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 stringWell,
Well,
followed by the value of thename
name
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. - 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 thesecretNumber
secretNumber
variable. - The
for guessesTaken in range(1, 7):
for guessesTaken in range(1, 7):
statement will create a for loop that will run six times. TheguessesTaken
guessesTaken
variable will store the number of times the loop has run. - 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 stringTake a guess. You have
Take a guess. You have
followed by the value of theguessesTaken
guessesTaken
variable subtracted from 7, followed by the stringguesses left.
guesses left.
to the console. - The
guess = int(input())
guess = int(input())
statement will prompt the user to enter a number and store the value in theguess
guess
variable. - The
if guess < secretNumber:
if guess < secretNumber:
statement will check if the value of theguess
guess
variable is less than the value of thesecretNumber
secretNumber
variable. - The
print('Your guess is too low.')
print('Your guess is too low.')
statement will print the stringYour guess is too low.
Your guess is too low.
to the console. - The
elif guess > secretNumber:
elif guess > secretNumber:
statement will check if the value of theguess
guess
variable is greater than the value of thesecretNumber
secretNumber
variable. - The
print('Your guess is too high.')
print('Your guess is too high.')
statement will print the stringYour guess is too high.
Your guess is too high.
to the console. - The
else:
else:
statement will run if theif
if
andelif
elif
statements are false. - The
break
break
statement will end the loop. - The
if guess == secretNumber:
if guess == secretNumber:
statement will check if the value of theguess
guess
variable is equal to the value of thesecretNumber
secretNumber
variable. - 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 stringGood job,
Good job,
followed by the value of thename
name
variable, followed by the string! You guessed my number in
! You guessed my number in
, followed by the value of theguessesTaken
guessesTaken
variable, followed by the stringguesses.
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