Python Intermediate #6 - Using randint function for addition and subtraction practice

In a previous post, we obtained two random integers from 0 to 9 and had the user enter the product of the two integers.  The program then notified the user if they had the correct answer.  If they did not, the program would provide them with the answer.

This program will be similar except that subtraction and addition will be used instead of multiplication.  The program will do the following:

  • Choose a random integer from 0 to 1.  If the integer is zero, addition will be used.  If the integer is 1, subtraction will be used.
  • Choose two random integers from 0 to 9.
  • Determine the maximum and minimum of these numbers if we are subtracting.  We do this since we don't want to subtract a larger number from a smaller number at this time since we don't want negative numbers for this practice.
  • Add the two numbers if we are using addition.  Subtract the two numbers if we are using subtraction.  
  • Ask the user for their answer to the addition or subtraction problem.
  • Inform the user if they are correct or of the correct answer.
The code used for the program is shown below.  The code is wrapped in a while loop so that the user can continue to practice until they are ready to quit.  


The results of a simulation of the code are shown below.  The code seems to work for correct and incorrect answers and allows the user to continue or quit.  



Comments