Developing a Multiplication Test Using Python

 This Python program demonstrates the use of the random module in Python, lists in Python, for loops in Python, and if statements in Python.  The program was created using Spyder as part of the Anaconda package.

A video of this demonstration is shown here:


In the first four lines of code shown below, the following is being performed.

First, I want to get random integers to multiply for my test so I bring in randint from the random module.

Next, I ask the user how many problems will be on test and store it as "num_probs".  I do this by using an input statement and assigning it as an integer (int).

I then make two empty lists, "list_vals_x" and "list_vals_y".  The square brackets indicate that they are a list.


I then do two for loops to populate "list_vals_x" and "list_vals_y" since I want to multiply x * y.  The for loop causes a repeat for the number of questions that was input by the user.  For each pass through the loop, a random integer from 0 to 9 is developed and appended to the list.  The for loops for assigning x and y values is shown below.


In the next 2 lines of code, I print my lists.  I did this to check that the lists were populated correctly and that the program is running correctly.  


I then initialize the number of correct answers to zero prior to entering a for loop that does the following:

  • It determines the correct answer for x * y for each pass through the loop ("answer")
  • It asks the student for his or her answer each pass through the loop ("test_ques")
  • It checks whether the student got the correct answer ("answer" = "test_ques").  If so, the number of correct answers, "num_correct" is incremented by 1.



Finally, the number of correct answers along with the percent correct is printed.  



The entire code is shown below:



A simulation of the program with three problems is shown below:





Comments