How I wrote a simple numbers game to play against the computer

In my last post I discussed how I entered a coding competition to create a simple numbers game in Python. The link for the post for the simple game that I programmed can be found here: How to program a simple numbers game using only numpy and pandas | by Tracyrenee | Python in Plain English | Feb, 2021 | Medium

Since I posted about the simple game that I have written, I modified the program so that one player plays against the computer. The way one person can play the computer in this game is to install a random integer generator that will create a number between one and nine, which takes on the role of the second player.

Python creates random numbers with the random module. Random number generators use mathematical formulas that transfer a set of numbers to another one. In Python I used the randint() function to create random integers between 1 and 9.

The challenge statement for this program reads as follows:-

“The Challenge

There are two players.

Each player writes a number, hidden from the other player. It can be any integer between 1 and 9.

The players reveal their numbers.

Whoever chose the lower number gets 1 point, unless the lower number is lower by only 1, then the player with the higher number gets 2 points.

If they both chose the same number, neither player gets a point.

This repeats, and the game ends when one player has 5 points.

The challenge is to write a script to play this game. Knowing the rules and all your opponent’s previous numbers, can you program a strategy? (And, no — return random.randint(1, 3) is not a strategy.) You should really try playing this first with your friends — you’ll see there’s a deep human element to predicting your opponent’s choice.

Is it possible to program a strong strategy?”

I wrote the program using Google Colab, which is a free online Jupyter Notebook that has Python and several libraries already installed on it. One thing I have discovered about this platform, however, is the fact that the many current updates on the libraries are not installed. Fortunately, the functions used in this program are all in python so there is no need to install any updates onto Google Colab.

Once I created the program on a Jupyter Notebook, I installed the libraries I would need to execute the program. I imported pandas as a matter of course because it enables manipulation and transformation of a dataframe. I also imported numpy, which is a library that supports algebraic functions. Finally I imported random because I would need it to generate a random integer:-

After I imported the libraries, I defined the variables and initialised them:-

Once the initial variables were defined and initialised, I created the body of the program. This program utilises two while loops and several if statements.

The player is prompted to enter data, which is necessary to keep the game going. When the game finishes, the player is asked if he would like to play again. If the player wishes to play another game then the while loop initiates another game. If the player doesn’t want to play another game then the game stops:-

I asked my boyfriend to test the program, which he kindly did. Below are the prompts and responses for the three games I asked him to play as part of the testing process:-

When the game concludes, statistics of the winner is prepared. The variable, winner, contains the winners during the game, as each winner is appended at the end of each game.

I counted the values of the winners and since there are only two players then there could only be two classes to count.

For clarity, I calculated the percentages for each class as well:-

The code for this post can be found in my personal GitHub account, the link being here: Games/Python_coding_challenge_List_winners_Random_Numbers_Game.ipynb at main · TracyRenee61/Games (

I have over 46 years experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store