Unit 4 Task 12 - Solution

Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.

Solution - Task 12: Three addition questions

← Back to task


One possible program:

import random


def ask_addition():
    a = random.randint(1, 12)
    b = random.randint(1, 12)
    ans = int(input(f"What is {a} + {b}? "))
    return ans == a + b


def main():
    score = 0
    for _ in range(3):
        if ask_addition():
            score += 1
    print("Score:", score, "/ 3")


main()