Description
In this nugget of code, I essentially recreate a game that I once played as a kid. This game was to help in the development of mental arithmetic for children by simply challenging them with a random assortment of questions based on a selected times table. I developed this game to be played within the terminal/console. Well lets dive into the code!
Here I import all the packages that I need for the game to work. There are no complex math or stats calculations therefor I am able to use python's built in mathematical functions. I chose to import the "random" module to help randomise the ordering of the questions to truly test the player. I also import the "time" module as to keep a record of their score.
Next I add input functions in order to prompt the player for what times table they would like to play and also the number of iterations too! I have enclosed these input functions within "while" loops because the player is a human (hopefully!) and we are all known to make mistakes. That being said there are a number of "try" blocks within the loops in order to validate the input from the player. Once the validations are satisfied, the loop is closed and the newly assigned variable is carried forward. I have added a default of 12 iterations should the player not be sure and enter anything.
Next I add input functions in order to prompt the player for what times table they would like to play and also the number of iterations too! I have enclosed these input functions within "while" loops because the player is a human (hopefully!) and we are all known to make mistakes. That being said there are a number of "try" blocks within the loops in order to validate the input from the player. Once the validations are satisfied, the loop is closed and the newly assigned variable is carried forward. I have added a default of 12 iterations should the player not be sure and enter anything.
Here I use a very similar concept to the code block before but here I don't set any "try" blocks and only accept 2 possible inputs. This is just to prepare the user before the game starts by welcoming them and ensuring they are ready to start the game instead of just diving straight into it. This seemed like a sensible thing to do as to provide a more pleasant user experience.
Here I use a series of lists in order to hold the questions and their answers. I run these in the same order in order to ensure each question index is aligned to the answer index.
This is where the main chunk of code running the game is held. The game begins after the player has verified that they are ready to play and the code has populated the lists of questions and answers. The game starts off with a 3 second count down, after which the timer begins and the user is presented with the first question.
While the user answers the questions, firstly, the code identifies whether the answer given is a number (as expected) and then appends the question and answer to a list depending on whether the answer is correct. There are two lists, correct answers and wrong answers. Instead of simply counting how many the user got wrong, storing the failed results in a separate list will allow the use to work on the questions that they answered incorrectly as the game will display these at the end! (Shown in next code block).
Once the user has answered all the questions, the game "stops" the timer. The time elapsed is not calculated by an actual timer but is calculated by taking the difference in timestamp from the time module from when the user started and right at the point when the user finishes.
While the user answers the questions, firstly, the code identifies whether the answer given is a number (as expected) and then appends the question and answer to a list depending on whether the answer is correct. There are two lists, correct answers and wrong answers. Instead of simply counting how many the user got wrong, storing the failed results in a separate list will allow the use to work on the questions that they answered incorrectly as the game will display these at the end! (Shown in next code block).
Once the user has answered all the questions, the game "stops" the timer. The time elapsed is not calculated by an actual timer but is calculated by taking the difference in timestamp from the time module from when the user started and right at the point when the user finishes.
Within this code block, the amount of time taken to take the test is calculated using a "divmod" function. This is because the "time.time()" method only returns the number of seconds at that given time. That way, if the user were to take longer than a minute, "1 minute 35 seconds" is a lot easier to interpret than "95 seconds".
I have used the sleep method in order to simulate a "loading" experience as the user waits shortly for their results. I thought this would be a nice touch as it adds a bit more suspense when awaiting results and hoping that they beat their personal best. I have only added this in for 3 seconds, anything more than that I thought to be excessive.
At the very end, the user is shown their results. They are provided with a percentage detailing the proportion of questions that they answered correctly. They are also shown the number of correct answers they had out of the total number of questions. The same is done for the answers that they answered incorrectly. In addition to this, the player is also provided with a list of questions and answers that they got wrong and perhaps should work on before attempting the game again.
I have used the sleep method in order to simulate a "loading" experience as the user waits shortly for their results. I thought this would be a nice touch as it adds a bit more suspense when awaiting results and hoping that they beat their personal best. I have only added this in for 3 seconds, anything more than that I thought to be excessive.
At the very end, the user is shown their results. They are provided with a percentage detailing the proportion of questions that they answered correctly. They are also shown the number of correct answers they had out of the total number of questions. The same is done for the answers that they answered incorrectly. In addition to this, the player is also provided with a list of questions and answers that they got wrong and perhaps should work on before attempting the game again.
Improvements and Ideas
With regards as to how this script can be improved, there are more than plenty of ways but I will only discuss a couple improvements and ideas that stood out to me most. I think it could have been improved by replacing the use of lists with dictionaries. This would make handling the data easier. I would achieve this by using the question as the key and the answer being the value assigned to that key.
An Idea I had was to restructure the game to be object-orientated where the game itself is an object and interacts with questions that are objects themselves, storing the answer as a value and their string format held in the __str__ method.
A final idea would be to add an SQLite database to interact with the script so that the results can be stored in a database. This data can then be analysed to monitor progress in different times tables as well as overall progress.
An Idea I had was to restructure the game to be object-orientated where the game itself is an object and interacts with questions that are objects themselves, storing the answer as a value and their string format held in the __str__ method.
A final idea would be to add an SQLite database to interact with the script so that the results can be stored in a database. This data can then be analysed to monitor progress in different times tables as well as overall progress.