In this post, I develop a timed multiplication test using Python. This post is an enhancement to a previous post where the test was untimed. The link to the previous post is below.
Untimed Multiplication Test in Python
The accompanying video for this post is located here:
Timed Multiplication Test in Python Video
This program asks the user to enter the number of problems
and to also enter the amount of time (in seconds) that is allowed for the test.
The “randint” method from the random module is imported as
was done in the previous multiplication test.
Additionally, the time module is also imported.
The user is then prompted for the number of questions on the
test and lists are developed (“list_vals_x” and “list_vals_y”) to store the
numbers to be multiplied.
The number correct (“num_correct”) and the number of problems
attempted (“count”) are initialized to zero prior to entering the loop.
The user is then prompted for the number of seconds allowed
for the test. Using methods in the time
module, two variables are initialized with the current time. These variables are “beg_time” and “now_time”. The variable, “beg_time” will remain
unchanged during the execution of the program since it will be used to compute
how much time has elapsed. The variable,
“now_time”, is updated with each multiplication problem to determine if there
is still time remaining to move to the next problem.
Much of the code existing in the while loop of this program
is similar to what was done in the untimed multiplication test. However, to incorporate the time element,
some modifications were required. First,
the while loop is executed while there are still problems to be answered. However, it can also be exited if the time
has expired by setting “count” to a very large number. The test to see if time is remaining is shown
by the if statement on line 38 of the code.
If time is remaining, the solution to the problem is computed and the
user is asked to input their solution.
If it is correct, the number correct (“num_correct”) is incremented by
1. If it is incorrect, the number stays
the same. The for loop in line 37 of the
code allows for incrementing through the lists of x and y values.
After the answer (either correct or incorrect) is provided,
the current time is once again extracted in line 46 of the code. The computation to determine if there is time
remaining once again occurs in line 38 of the code. If time is remaining, the next question is attempted. If no time is remaining, “count” is set to a
very high number to exit the while loop.
This code is set up so that once a problem is started, the time is not
checked until that problem is completed so the timer only determines whether the
user can move to another problem, but it will not exit the current problem. The while loop is exited either when all of the problems have been attempted or if the time runs out.
Python timer, while loop in Python, if statement in Python, for loop in Python, time module in Python, random module in Python, timed multiplication test, timed math test
Comments
Post a Comment