In this post, we will learn to create a python program for maths quiz.
Through this program, user would be given a choice of maths operation from given options. Further to that user is provided with a question of user-chosen operation between 2 randomly generated numbers. This is repeated until the user chooses an option to exit. Detailed requirements can be seen in the section - How It Should Work. There are paths associated when a user enters an option that does not exist. Once the quiz is over, a count is provided for the total questions answered and correctly answered questions.
If you wish to learn the concepts and see the detailed steps to arrive at the solution, refer to my video on youtube
Python for loop with dictionaries, while loop, random method, inputs, if-else, type conversion, f-string, number operation
How It Should Work
Display options to the User. Create a dictionary with option number as the key and operation name as the value. Loop through the dictionary and display options to the user.
Prompt user to enter the option
Validate that if the user enters an option greater than 5 or less than/equal to 0, an invalid message is given and the user is given the option again until the user enters an option between 1 to 5. This can be done using a while loop.
Once user enters the number between 1 to 5, the user gets out of the while loop.
Start next while loop which runs until the user wishes to exit from the quiz and chooses option 5
Within the while loop, generate 2 random numbers and perform user-chosen operation to get the correct answer
Also, ask question to the user by showing question with 2 numbers and operation between them with equal to sign.Use input method to save user answer in a variable.
Finally, display total count for questions answered and correctly answered questions.
For demo refer to the section How It Should Work