Project: the revision robot

A robot that asks you questions, marks your answers and tells you what to revise.

F13.9Exam preparationGCSE25 min

Covers OCR J277 2.2.1 Programming fundamentals · AQA 8525 3.2.2 Programming concepts · Edexcel 1CP2 6.1.3 Algorithms into code

Do this lesson in the simulator

The last task of the course builds something you can use for the rest of it: a robot that tests you. It holds a bank of questions, asks them, marks your answers, keeps the ones you got wrong and asks those again, and tells you which topic to revise next. It is retrieval practice (lesson F13.8) with a buzzer.

It also happens to use nearly everything you have learned: lists and records, loops, selection, string handling, input, functions and the robot itself.

What it does

  1. Ask each question in the bank in turn.
  2. Read the answer, and mark it ignoring capitals and spaces at the ends.
  3. Say whether it was right, and if not, give the answer.
  4. Sound a high note for right and a low one for wrong, and set the LED green or red.
  5. At the end, print the score and the topic with the most wrong answers.

The question bank

Each question is a record with a topic, a question and an answer, so a new one is one line:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

BANK = [
    {"topic": "Networks", "q": "What does DNS turn a domain name into?", "a": "an IP address"},
    {"topic": "Data", "q": "How many bits in a byte?", "a": "8"},
]
for item in BANK:
    print(f"[{item['topic']}] {item['q']}")

Run this in the simulator

The question bank as a list of two records, each with a topic, a question and an answerBANK: a listBANK[0]"topic"Networks"q"What does DNS turn adomain name into?"a"an IP addressBANK[1]"topic"Data"q"How many bits in a byte?"a"8item["q"] is the question of whichever record the loop is on;a new question is one more record on the end of the list.
Every record has the same three keys, so one loop can ask any of them: the key on the left, the value beside it. Adding a question never means changing the code, only adding a record.

Marking an answer

A marker that only accepts an exact match is useless: 8 and An IP address are both right. Clean both sides before comparing:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def same(given, wanted):
    return given.strip().lower() == wanted.strip().lower()

print(same(" An IP Address ", "an ip address"), same("7", "8"))

Run this in the simulator

The given answer and the wanted answer each go through strip and lower, and come out the samegiven" An IP Address ".strip()"An IP Address".lower()"an ip address"wanted"an ip address".strip()"an ip address".lower()"an ip address"==Truespaces at the ends gone, then capitals"7" and "8": still different
Both sides are cleaned the same way before they are compared, so a stray space or a capital letter cannot turn a right answer into a wrong one. A wrong answer is still wrong after cleaning.

Plan it first

  • a function to ask one question and return True or False;
  • a loop over the bank, counting the score and collecting the ones that were wrong;
  • a second pass over the wrong ones, if there are any;
  • a count of wrong answers per topic, then the worst one;
  • the sounds, the LED, and the report at the end.

Task: the revision robot

Ask every question in BANK in turn. For each one, print [<topic>] <question>, read the answer with input(), and mark it ignoring capitals and spaces at the ends. Print right for a correct answer, or wrong, it is <answer>. Play a 880 Hz note for right and 220 Hz for wrong, each 0.2 seconds. At the end print score: <n> of <total> and revise: <topic>, the topic with the most wrong answers, then set the LED green if you scored more than half and red if not. The answers are typed for you.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

BANK = [
    {"topic": "Networks", "q": "What does DNS turn a domain name into?", "a": "an IP address"},
    {"topic": "Data", "q": "How many bits in a byte?", "a": "8"},
    {"topic": "Networks", "q": "Which protocol sends email?", "a": "SMTP"},
    {"topic": "Security", "q": "What does a firewall check?", "a": "traffic"},
]

Challenges

  1. Ask the wrong ones again at the end, until they are all right.
  2. Add your own questions from your weakest topic, and run it before school.
  3. Save the score to a file each time, so you can see whether you are improving.