Reaction Time Test!
How fast are your fingers??
This input-based reaction test was one of my first game-like projects as a beginner in elementary school, which is why it is the perfect game for any novice to pick up and show-off their newly acquired coding skills!
The skills that will be covered in this project includes:
- Importing libraries
- For loops
- inputs
- Using variables
- If statements
Supplies
For this project, we will be coding in Python, so it is necessary to download a python version which you can access via a Python IDE.
To download the latest python versions: https://www.python.org/downloads/
The Python IDE I used was IDLE, which you can access upon downloading a python version on your device.
Importing Time
Importing libraries is like picking tools out of a toolbox. Python has a directory of functions, classes, and other tools to help coders write code without having nothing to base their code off of.
We import libraries with the "import" statement above. In this code, we need to import time because it is a library that can be used to measure the time a code/input takes to run! That's what we will be using for this project, but the time library can also be used to assign a given amount of time to a function.
For Loops
A for loop executes a sequence of code repeatedly(based on the number of times you tell it to). For example a "for i in range(5)" loop would run the code 5 times.
However, it can also be used to run through a list or a tuple(which are sequences that store values). For example, "for fruits in fruits" would print all the fruits within the list named "fruits".
For this code's purposes, I chose to execute the code 10 times before stopping. So, it will restart the time, will print the initial print statement, acquire input, and read the if statement 10 times!
**Notice: anything outside the for loop will only run once. For example, the "welcome" print statement at the top of the code will only print once.
Variables and Inputs
Within the for loop, we have added a variable called "start" which starts measuring the time after initializing the for loop. Another variable, called "reaction_time" subtracts the current time from the initial start of time to determine what the reaction time is for our game.
Yet, within those two variables, we have our input statement. This records the input, or in this case, records pressing the enter key(empty string), when the code starts.
Remember, because our for loop repeats ten times, the game will run ten times. This means that the code will prompt to press the enter key a total of ten times.
If Statement
Just to make the game a little more responsive, I also added an if statement that analyzes the input.
An if statement is exactly what it sounds like -- it checks if a condition is true, before running what is in it.
In this case, if the reaction time was greater than two, the code would print "That's way too slow!", followed by the final print statement. However, if it is less than two seconds, the code will ignore the if statement, and just return the final print statement.
Personalize!
Congratulations! You just made your very own reaction test game! See what you can add or change to this code to make it your very own! For example, using the skills in this project, you could try to add multiple different types of input-based tests or change the if conditions. You could even experiment with "time" such that it waits before executing the next loop in the series of 10 loops.