# Times-table true or false: a free micro:bit lesson from Maze Education
# https://maze.education/microbit/lessons/times-table-quiz
# MicroPython for the micro:bit Python Editor: https://python.microbit.org

# Times-table true or false: A if the fact is right, B if it is wrong.
from microbit import *
import random

score = 0
questions = 0
while True:
    first = random.randint(2, 9)
    second = random.randint(2, 9)
    answer = first * second
    if random.randint(0, 1) == 1:
        answer = answer + random.randint(-3, 3)
    display.scroll(str(first) + "x" + str(second) + "=" + str(answer))
    button_a.was_pressed()
    button_b.was_pressed()
    while True:
        if button_a.was_pressed():
            said_yes = True
            break
        if button_b.was_pressed():
            said_yes = False
            break
        sleep(20)
    if said_yes == (answer == first * second):
        score = score + 1
        display.show(Image.YES)
    else:
        display.show(Image.NO)
    sleep(500)
    questions = questions + 1
    if questions == 10:
        display.scroll("Score " + str(score))
        score = 0
        questions = 0
