~ Step by Step challenge and tutorial for creating a maths (arithmetic) quiz in Python 3 (with scores written to file)


Creating a Maths Quiz - complete with writing names and scores to file!

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!

Part 1: Get started with creating the Maths quiz

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!

Demo of expected solution

Solution Video

Part 2: Ask for the user's name.

Check user's response and respond accordingly. (for instance, responses like: "that's right", or "that's wrong")

Demo of expected solution

Solution Video

Part 3: Validation is important. Validate the user's name entry to allow NO numbers!

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.

Demo of expected solution

Solution Video

Part 4: Calculate the score - incrementation

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.

Demo of expected solution

Solution Video

Part 5: Put it all in a function called 'quiz'

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.

Demo of expected solution

Solution Video

Part 6: Finally, let's save these names and scores to a text file.

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

Demo of expected solution

Solution Video

All Solution files and answers are in the "Members Only" Box Drive.

Please sign up for more