Paul's Python CoursePPC

Project 8 of 20, for Caitlin

Quiz App

Caitlin's Thursday quiz night, with rounds on music, food and games: questions in a JSON file, shuffled every week, asked, marked and scored, mostly with lists made out of other lists.

About an hour and a half, in 18 steps.

Sign up free to open itLog in

Act 2 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • CaitlinI'm running the class quiz on Thursday, and my question sheet's got a coffee ring over half the answers.
  • CaitlinI want the questions in a file, in a different order every week, with marking nobody can argue with.
  • CaitlinLast time Yahya argued that piano and the piano were different answers, and he won the argument but not the quiz.
  • PaulOnce they're loaded it's mostly shuffling and marking, and nearly all of that is lists made out of other lists.

Caitlin's questions are in questions.json: a list of dictionaries, each with a round, a question, an answer, and sometimes also, a list of other answers she'll accept. Write quiz.py, with a comprehension at the heart of each of the first four:

  1. round_names(questions) returns the different rounds, in alphabetical order.
  2. round_sizes(questions) returns a dictionary of each round's name and how many questions it has, in the same order.
  3. in_round(questions, name) returns the questions in that round, in the order they're in the book. Capitals, and spaces at the ends of name, don't matter.
  4. answer_sheet(questions, answers) returns a line for each question, numbered from 1: 1. time: right, or 2. the piano: you said keyboard.
  5. is_right(given, question) is True if given is the answer or one of its also, ignoring capitals and spaces at the ends. score(questions, answers) says how many are right.
  6. main() prints the rounds, then asks Which round? until the answer is one of them; anything else gets No round called Sport. It shuffles that round's questions with random.shuffle, prints each one numbered and reads the answer after > , then prints Answers:, the answer sheet, and Score: 3 out of 4.
What it prints
Rounds: Food (4), Games (4), Local (4), Music (4)
Which round? music
1. What does a metronome keep?
> time
2. Which instrument has 88 keys?
> keyboard
3. How many strings does a standard guitar have?
> six
4. What do you call a group of four musicians?
> Quartet
Answers:
1. time: right
2. the piano: you said keyboard
3. 6: right
4. a quartet: right
Score: 3 out of 4

What you learn

What's in it

  1. Lists and dictionaries in a file
  2. Written as JSON
  3. The question book
  4. A list made from a list
  5. A list in one line
  6. Keeping only some
  7. The long answers
  8. Choosing what to write
  9. Two places for an if
  10. Sets and dictionaries in one line
  11. The same key twice
  12. A different order every week
  13. What shuffle hands back
  14. How Paul would do it
  15. Fix Yahya's version
  16. Make the quiz
  17. Ship it