A Simple Guessing Game in Python

Arpit Omprakash
Byte-Sized-Code
Published in
3 min readMay 8, 2020

Many people start with learning to code and give up mid-way as they don’t know what to do further or get frustrated while doing the numerous painful but straightforward exercises online that gives them no joy upon finishing. It is just another easy assignment completed.

People are encouraged to develop programs that are enjoyable and challenging to keep them motivated to learn more.

Here is the outline for one such exercise, a simple game designed to be coded and enjoyed in python.

Keeping in mind the need for originality and the importance of developing logical thinking in programmers, everyone is encouraged to use the outline below to write their versions for the game. However, a refined implementation is given to compare your code to and (hopefully) inspire you to revise and improve your code.

The Game

The game is pretty simple. The computer randomly chooses a number, and the user has to guess the number that the computer has selected in a specified number of tries. The user is informed whether the number guessed by the user is greater or smaller than the required number to be guessed.

An outline

There are many ways to accomplish this simple task, and one such way is outlined in the flowchart below.

Flowchart for the game

As seen above, the game passes through some significant checkpoints that are stressed below:

  • Initialization: The part where the computer chooses a number
  • Check the number of tries: checks if the number of attempts has exceeded. If the user exceeds the number of maximum tries, the program should terminate.
  • User input: The user inputs a guess. Note — The program must check if the number of tries has exceeded before this
  • Check guess: If guess matches the answer, the program exits. Otherwise, the program should compare the guess with the answer and let the user know how the guess compares to the answer.

Points to Ponder

Here are a few points that I came across while implementing the game:

  • What should be the number of tries?
    As the game is random (i.e., the computer chooses a number randomly), what should be the number of tries that a user should get to guess the number?
  • Optimal strategy
    Can you devise an optimal strategy that helps you guess the number correctly almost every time? I have one which I think is optimal, comment yours so we can discuss more!
  • The difficulty of the game
    Almost all popular games provide different levels that define how easy/hard it is to win a game. What can be a similar measure for this game? Increasing or decreasing the number of tries seems a bit trivial. Other suggestions are welcome!

Conclusion

Here is my take on the game. Let me know what you think of it, and if you enjoyed reading this article and implementing the game for yourself.

Sample Implementation

Before you go…

Connect with me on Instagram and Facebook.
If you liked this story, hit the clap button (you can clap up to 50 times with just one button!) and follow me for more such articles!

Share, recommend, and comment away! Engagement helps us communicate and be more human!

--

--

Arpit Omprakash
Byte-Sized-Code

I'm a Programming and Statistics enthusiast studying Biology. To find out if we have common interests, have a look at my thoughts: https://aceking007.github.io/