Building Guessing Game with Python
Programming

Building Guessing Game with Python

Building "Guessing Game" With Python- A Simple Python Project.



Introduction

Python is one of the most popular programming Language we have.

The applications of Python are very wide and cut across various aspects of tech, including Software Development, Software Engineering, Data Science, Data Storytelling , Data Engineering and Artificial Intelligence at large.

There are lots of amazing Projects recommended for Python Beginners to explore.


Python Projects For Beginners

Some of these are:

-Mad Libs

- Guess the Number Game

- Rock, paper, scissors

- Hangman

- Countdown Timer

- Password Generator

- QR code encoder / decoder

- Tic-Tac-Toe

- Binary Search, etc.

This article contains a well detailed Step-by-Step process to Programme a Number-Guessing Game Using Python.

Guess-The-Number Game

The first thing is to comprehend the concept of the game.

Concept of the Number-Guessing Game

The idea is to build a user friendly game.

This game uses a function "guess_number()" which when called, brings an Input for the user (to input a value).

There is an instruction that comes with the input telling the user to input a number between 0 and 100.

The Programme has a number in mind, and if the user guesses the number correctly, the Programme returns "Just right, Congrats!". But if the user guesses the number wrongly, the Programme tells the user if it’s too high or too low and asks to try again.

If the user inputs a text instead of a number, the Programme returns the text and asks the user to input a number.

This might not sound very interesting but trust me, Writing a program that works fine gives you a joy that’s second to none.

Now, we’ll discuss the step-by-step method I used to build the Programme.





The Step-by-step Process

1. Set Up Your Coding Environment.

Their are different options of environments we can use but the best and most widely used are Jupyter notebook (From Anaconda) and Google Colab.

I will be using Jupyter Notebook

2. Install NUMPY.

There are different python libraries. For this game, we’ll be using a very basic Python Library, NUMPY.

To install you’ll have your data connection on, then go to your Command Prompt or Anaconda Command Prompt and write:

pip install numpy

or

conda install numpy (If you’re using Anaconda)

2. Import NUMPY!

Following the successful installation of Numpy is it’s importation to the Jupyter notebook or the Text Editor you’re using.

To do this, write:

import numpy as np




3. Define your function:

The next step is to define the function we’ll be using to call the game for action. Anything can be used, here I used "guessing_game".

To do this, write:

def guessing_game():




4. Add the While Loop

Here, the while loop is a conditioning loop, telling the Program to keep running (telling the user to keep trying) till the user guesses right.

To do this, go to the next line, add an indentation/required spacing (this might have been added automatically), then write:

while True:



4. Add the For loop:

Here, the for loop refers the Programme to a set of random integers between range 0 and 100. (Remember the Idea of the game).

To do this, go to the next line, add indentation/required spacing, the write:

for x in [np.random.randint(0,101)]:

(Note: the 101 here is exclusive.)



5. Introduce the Input

The input here is like a long rectangle for the user to input values. The input will be introduced with a name which can be anything. Here, mine is "answer". A string/text is passed alongside the input which contains instruction for the user to interact with the input.

To do this, go to the next line, add indentation, then write:

answer = input(’Guess Number Between 0 - 100: ')


6. Create the first "if" condition

The first "if" condition here is used to tell the program what to do if the input is a digit or otherwise (a text, etc.)

To do this, go to the next line and without an indentation (on the same line as 5 above), write:

if answer.isdigit():


7. The Second "if" condition

This is a condition inside the condition above. In this context, it’s used to tell the program what to do if the digit input is within the range 0 to 100, or not.

To do this, go to the next line, add indentation then write:

if int(answer) in range(0,101):



8. Third "if" condition:

This is a condition under the 2nd "if" condition above, that is: what the program should do if the digit input, which is in the required range of values is Guessed Right. Here, it tells the Programme to return "Just Right, Congrats!", then stops the Programme.

To do this, go to the next line, add an indentation, then right:

if int(answer) == x:

print ('Just right, Congrats!')

break

(Note: Take note of the indentations used and omitted! The "break" is what breaks the While Loop (telling the program to stop if right))



 9. The elif co-condition

This is giving another condition like the one above condition. Here, it tells the Programme to return "Too high! Try again!" if the number guessed is too high.

To do this, fo to the next line, un-indent (using backspace) to the level of the third "if" above, then write:

elif int(answer) > x:

print ('Too high! Try again!')




10. The first "Else":

The "else" ia used to break a condition, I mean what happens if an if or elif condition is not fulfilled. Here, the first "else" is used to tell the program what to do if the input is neither correct nor too high, that is, too small.

It tells the program to return "Too small! Try again"

To do this, go to the next line, on the level of the "elif", Write:

else:

print ('Too low! Try again!')





11. The second "Else"

This time around, the "else" is related to the second "if" (they’ll be on the same level), and it tells the program to respond "Number out of Boundary" when the digit input is not within the range 0 to 100.

To do this, go to the next line, un-indent to the level of the second "if", then write:

else:

print(’Number out of Boundary!’)



12. The third "else"

This is related to the first "if" condition, telling the Program what to do if the input is not a digit. Here, it returns the text and tells the user to input a number!

Here comes the end of the Code.




General Notes:

1.The #Guessing Game is a form of commenting in python.

2. The int() is used to ensure the input is an integer.

3. The 'x' is the number the Programme has in mind for the user to guess right.

4. Code "guessing_game()" is always used to call for the programme’s action as defined by the function.

5. Indentations omitted or mis-added could lead to en error.

6. Cases of the codes are important (lower or upper case)

7. The kinds of brackets used are important.

8. There might be need to restart Kernel whenever the program refuses ro run.

To do this, check for "Kernel" at the tool bar on top, click on it, then click on "Restart". Then re-run your program.

I'm sure the project was really enlightening and I know there'll be some observations, clarifications or questions you might need to get across to me.

You can reach and follow me on Github , LinkedIn, Twitter ,Instagram and WhatsApp.

See you next time!


  • Abdulquddus Ajibade
  • Mar, 20 2022

Add New Comments

Please login in order to make a comment.

Recent Comments

Be the first to start engaging with the bis blog.