Build a Guessing Game with Python
Tags: python, guess, game, python3
Published: 2020-05-18 22:41:55

In this article, we will be building a simple guessing game using Python. This was a task done during the StartNG Beginner Software Development Training. The game would have three modes corresponding to levels of difficulty.

In the Easy mode, the player would try to guess the random number generated by the program between 1 to 10. The player would have 6 tries at guessing the correct number.

In the Medium mode, the player would try to guess the random number generated by the program between 1 to 20. The player would have 4 tries at guessing the correct number.

In the Hard mode, the player would try to guess the random number generated by the program between 1 to 50. The player would have 3 tries at guessing the correct number.

Before heading into the actual solution of the task, it is usually best practice to make a rough sketch (pseudo-code) of how the program should be. 

To achieve the aim of the program, we would need to have a means of generating a random number. We could choose to select a particular number within each range to be the number, but that would not be fun. We need the number to change at every iteration of the program. 

So to generate the random number, we would be making use of a Python module called “random”. This enables us to have a different random number every time the game is played.

It is possible to use if/else statements for the three different modes of the game, but it is usually best practice to write functions wherever possible to make for more readable code. So we would be making use of functions.

First of all, we would be creating a function called guess_game. This function would have two parameters which are limit and number. Below is a pictorial illustration of the code.

![Guess_game function](https://cdn.hashnode.com/res/hashnode/image/upload/v1589839515303/NRuE4-yfa.png?auto=format&q=60)

A variable for the random number was created and the variable makes use of the “random” module. The “randint” built-in function of the “random” module is used to generate a random number between 1 and another variable (number) that corresponds to the highest number in the range from which the player should make their guess.

The function would need two arguments each time that the function is called. These two arguments correspond to the number of tries and the highest number in the range for the different modes. 

So in the case of the Easy mode, we would call the guess_game function and pass 6 as the argument for limit, which is the number of tries a player has to make the correct guess. 

For the other parameter number, the argument would be 10, since that corresponds to the highest number in the range of which the player should guess a number for the Easy mode.

In the rare case where our player enters anything other than a number, the try and except block handles that exception and tells the user that only numbers are allowed.

So we will pass the arguments for each mode, and this results in the functions below

![functions for different modes](https://cdn.hashnode.com/res/hashnode/image/upload/v1589839570553/FvGSMw8yM.png?)

After creating the functions that would enable the game to run smoothly, we would need a function that would allow the player to choose what mode they want to play in. For that, we would be creating a function called welcome.

![welcome function](https://cdn.hashnode.com/res/hashnode/image/upload/v1589839599499/BXwLkvkhE.png?)

Now we have a program that runs. Unfortunately, our program only runs once. You know what that means? If our game were to be a mobile app, our game would exit after one round and the user would have to launch the application to play a second round. 

I guess we do not want that. We would want to give our users the opportunity to choose to play again without having to launch the application every single time. 

Well, like always, we would create a function for that. We never get tired of creating functions, right? So we would create a try_again function.

![try_again function](https://cdn.hashnode.com/res/hashnode/image/upload/v1589839616714/YOn2DCLVT.png?auto=format&q=60)

We have to add the try_again function to the welcome function so that our player has the option to try again after one round of the game has ended without having to start the program entirely.

![final welcome function](https://cdn.hashnode.com/res/hashnode/image/upload/v1589839652223/8-I1Ue7VX.png?auto=format&q=60)

Now our players can choose to play the game as much as they want.
So voila, we have been able to use a couple of functions to build a simple guessing game. It was fun, right? Yeah, I sure had fun building it too.

Feel free to drop your comments, and questions below

The source code can be found here — https://github.com/ema-mimus/guess_game/blob/master/Guess_game.py