A number guessing game in python 3 for beginners

Hey, In this post I will show you how to develop simple number guessing game in python 3, This is focused for a beginner level python learner. Even though this is a simple game you can learn the basics of python programming language.

First, let’s examine how the game is structured

The Number Guessing Game

Preprocess

we set a secret number randomly within a range of 1,10

Inputs
  1. The Player Name
  2. The Guess Of Player. (Maximum 5 guesses)
OutPut

Our program out put can be based on the following logic

If the Number is higher than the user guess
           then, the program prints your guess is low,

else if the Number is less than the user guess 
           then, the program prints your guess is high.

else we can print out the player that he has won the game because player found the secret number
That’s All! Hooray….Let’s start coding in Python ūüôā

First, we need a random number within a range of (1,10) which will be the secret number. Take a look at the random module in python. The module can generate a random number for us, So We need to import that module into our program,

import random

Then we generate a random number using this module

secret=random.randint(1,10)

So our secret number is ready, Then we need a player name so that we can greet them, We have pythons print() function to print something on the screen and we have ¬†`input()` function to get the user input. Let’s get his name and greet him.

print("Give me Your Name")
name=input()
print("Welcome "+ name)

what we need to do next is taking the user input and match it to the secret number until the allowed number of guesses are over. So that we need a loop, Let’s use a for loop here and some print(), input() functions too. The input function returns a string, so we convert the input string to an integer like below,

guess=int(input())

Let’s combine our knowledge into¬†create the guessing part of our game.

for guesstaken in range(1,6):
	print("\nTake a Guess! "+ name)
	guess=int(input())
	if(guess<secret):
		print("Your Guess is low!")
	elif(guess>secret):
		print("Your Guess is high!")
	else:
		break

The above code snippet will try to take input from player and match for the secret number,

if ever the player guess matches the input then the break statement executes, a break statement will help us to get out of the loop immediately, Otherwise the loop continues for a specified number of times, Here we have maximum of 5 guesses, the `range` function will include the first parameter and exclude the second parameter. so range(1,6) will yield 1,2,3,4,5 Therefore a maximum of 5 guesses.

After the execution of the above code, we are in a state with¬†two options, either the user guessed it right and exited out of the loop, or ¬†The user tried maximum attempts he can try but didn’t guess it right.

So we need to check the validity of the answer once more, see the code below

if(guess==secret):
	print("Your Guess is correct " + str(guess))
	print("You Took "+str(guesstaken) +" Guesses")
else:
	print("You Took "+str(guesstaken) +" Guesses")
	print("The Secret was "+ str(secret))

if the guess is equal to the secret so that we can print the player won the game otherwise he lost his game.

That’s It we have finished¬†our game. Let Me know what you think in comments.

You can find the finished game with a user-friendly interface and clean code in Github.

See the complete code below after your experiments.

#!/usr/bin/python3
#Guessing game - Shobi P P

import random
secret=random.randint(1,10)
print("**********************")
print("Number Guessing Game")
print("**********************")
print("Give me Your Name")
name=input()
print("Welcome "+ name)
for guesstaken in range(1,7):
	print("\nTake a Guess! "+ name)
	guess=int(input())
	if(guess<secret):
		print("Your Guess is low!")
	elif(guess>secret):
		print("Your Guess is high!")
	else:
		break

if(guess==secret):
	print("***********You Win***********")
	print("Your Guess is correct " + str(guess))
	print("You Took "+str(guesstaken) +" Guesses")
else:
	print("***********You Lose**********")
	print("You Took "+str(guesstaken) +" Guesses")
	print("The Secret was "+ str(secret))

Programmer, Hacker, Trainor, Speaker, and Blogger. Opensource Lover. Built LeHacker.com.

Leave a reply:

Your email address will not be published.

Site Footer