📊 We use cookies & analytics to improve your experience. Learn more
Latest

Python for Absolute Beginners Module 5 · Lesson 14 of 15

0% Exit to course

Module 5 · Three projects and a shareable repo Lesson 14 of 15

Project 2 — Quiz from a file

Learning objective

Read questions from a text or CSV file, ask them one at a time, score the answers and print a result — the shape of nearly every exam and survey program.

The concept

A quiz program reads questions from a file, asks them one at a time, records the answers and prints a result. Its shape — read a set of items, present each, score the response, summarise — is the shape of nearly every exam, survey, form and interview-rating tool you will ever be asked to build.

Keep the questions in questions.csv with the columns question, correct answer, so adding a question needs no code change. Score into a list of dictionaries rather than a single running total, because that is what lets you print an answer-wise report at the end — "question 4: you said B, correct was C". Handle an empty answer by asking again rather than crashing, and shuffle the order if you want a second attempt to mean anything.

Common mistakes

The mistake is hiding the questions inside the code, which makes the program impossible to reuse and impossible to hand over, and the second is scoring into one number: a total of 6 out of 10 tells the user nothing about what to fix, and telling people what to fix is the point.

Worked example

Storing questions in questions.csv instead of inside the code means adding a question needs no code change — a design decision worth mentioning in an interview.

Your practice task

Add ten questions of your own, handle a blank answer without crashing, and print an answer-wise report at the end.

Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.

Key takeaway

A quiz program reads questions from a file, asks them one at a time, records the answers and prints a result.

Create free account or sign in

Almost there

15 lessons left of 15

Finish every lesson to unlock the certificate. Each one ends in a practice task — do them and the final project brief writes itself.