Here we take you through the steps involved in creating a simple Maths quiz, complete with validation, functions, loops and file handling. Try to solve the problems yourself, and then look at the answers (video). We've deliberately posted the answers in a video format so that they cannot be cut and paste. Much of your learning will come from typing code out for yourself!
Create 3 questions (math related of course) for the user to answer. To make it interesting, get the questions to be automatically generated (you will need to use random numbers for this) and plug in the different operators as well that could also be used randomly. Use operators like "+", "-", and even the newly learned "%!", if you can!
Check user's response and respond accordingly. (for instance, responses like: "that's right", or "that's wrong")
You can extend this validation to make it as complex as you like. You could also experiment with additional validation techniques you have come across before. For now, just ensure the user cannot input numbers.
What's a quiz without a score? In this part, get the score working. Add +1 to the score every time the user gets an answer right. Ensure the score is added up at the end of the three questions.
Modular programming is important. It's quite crucial to have code organised into functions for reasons you have covered in your theory lessons! As you put the existing code into a function, don't forget to get your indentation right.
Using the skills that you have already picked up, it shouldn't be too difficult to simply write the name of the user and their score to a text file called scores.txt
Please sign up for more