Creating a Number Guessing Game Using Python

Favour OlumeseFavour Olumese
3 min read

ECX 30 Days of Code and Design

Day 14

Guess the Number

Task

You ask a user to guess a number between 1 and 50. The user has a maximum of 5 tries. If the user guesses wrongly, provide an error message indicating whether their guess was above or below the actual number. If the user guesses correctly, congratulate him and show the number of attempts they had. If the user exhausts all their tries, tell him that he has exhausted his tries and end the game. E.g.:

>>> Enter a number.
user: 1
>>> Wrong! The answer is greater than 1.
user: 25
>>> Wrong! The answer is less than 25.
user: 14
>>> Wrong! The number is greater than 14.
user: 15
>>> Correct! You got the right answer in 3 tries.

My Approach

First, we would import the random module which we would use to randomly shuffle from 1 to 50 using the randint() function. We save the random number in the variable, random_num. We then print out the instructions the player is to follow. Next, we make use of a for loop, which allows the player 5 guesses before the loop breaks. The player is prompted to make a guess. If the number he guesses is greater or less than the random number, he is informed. If he is able to make the right guess, he is congratulated. If the player fails to make the correct guess in 5 tries, the loop breaks and the correct answer is displayed. A try except block is used to handle value error.

import random

random_num = random.randint(1, 50)
player_guess = ''

print(' Number Guessing Game '.center(40, '*'))
print('You have 5 tries to get the answer.')
print('Guess the number from 1 to 50.')

for i in range(5):
    try:
        player_guess = int(input('Guess the number: '))

        if player_guess < random_num:
            print('Wrong: The answer is greater than', player_guess)
        elif player_guess > random_num:
            print('Wrong: The answer is less than', player_guess)
        elif player_guess == random_num:
            print('Correct! You got the right answer in ' + str(i + 1) + ' tries')
            break
    except ValueError:
        print('Invalid input! Input only integers.')
if player_guess != random_num:
    print('Sorry! The answer is', random_num)

Game Play

********* Number Guessing Game *********
You have 5 tries to get the answer.
Guess the number from 1 to 50.
Guess the number: 12
Wrong: The answer is greater than 12
Guess the number: 24
Wrong: The answer is greater than 24
Guess the number: 36
Wrong: The answer is less than 36
Guess the number: 32
Wrong: The answer is less than 32
Guess the number: 30
Correct! You got the right answer in 5 tries

Play the game on Replit

0
Subscribe to my newsletter

Read articles from Favour Olumese directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Favour Olumese
Favour Olumese

Hello, I am Favour. I am a Python enthusiast. The Coding Process is a place where I document and consolidate what I learn on my journey towards becoming a programmer. Kindly join me. I am also a poet and non-fiction writer. You could visit https://favourolumese.medium.com to read my writings. Thank you.