In [ ]:
filename = '20170810-addition-list.txt'

In [ ]:
with open(filename) as f:
    for line in f:
        question, correct_answer = line.rsplit(':', 1)
        answer = input(f'what is {question}')
        # print(answer, correct_answer)
        if answer.strip() == correct_answer.strip():
            print('good')
        else:
            print('bad')

In [ ]:
with open(filename) as f:
    for line in f:
        question, correct_answer_to_ignore = line.rsplit(':', 1)
        answer = input(f'what is {question}')
        correct_answer = str(eval(question))
        # print(answer, correct_answer)
        if answer.strip() == correct_answer.strip():
            print('good')
        else:
            print('bad')

In [1]:
from random import randint, choice
from operator import add, sub as subtract, mul as multiply, floordiv as divide
from collections import OrderedDict
from functools import partial

In [2]:
[randint(1, 5) for _ in range(10)]


Out[2]:
[5, 3, 2, 5, 2, 3, 4, 5, 5, 2]

In [3]:
def math_quiz():
    operators = OrderedDict((
        ('+', add),
        ('-', subtract),
        ('*', multiply),
        ('/', divide),
    ))

    if False:
        prompt = f'Enter {", ".join(operators)}, or q to quit. '
        for operator_symbol in iter(partial(input, prompt), 'q'):
            if operator_symbol in operators:
                break
            print('try again')

    prompt = f'Enter minimum integer or q to quit. '
    for s in iter(partial(input, prompt), 'q'):
        try:
            minimum = int(s)
        except ValueError:
            print('try again')
        else:
            break

    prompt = f'Enter maximum integer or q to quit. '
    for s in iter(partial(input, prompt), 'q'):
        try:
            maximum = int(s)
        except ValueError:
            print('try again')
        else:
            break

    minimum, maximum = sorted((minimum, maximum))

    while True:
        operator_symbol = choice(list(operators))
        x, y = (randint(minimum, maximum) for _ in range(2))
        prompt = f'What is {x} {operator_symbol} {y}? (or q to quit) '
        answer = input(prompt)
        if answer.lower()[0] == 'q':
            break
        correct_answer = operators[operator_symbol](x, y)
        if answer.strip() == str(correct_answer):
            print('Correct!')
        else:
            print('Wrong!')

In [4]:
math_quiz()


Enter minimum integer or q to quit. -8
Enter maximum integer or q to quit. +5
What is -8 / 1? (or q to quit) -8
Correct!
What is 5 + -5? (or q to quit) 0
Correct!
What is -1 + -3? (or q to quit) -4
Correct!
What is 3 / -3? (or q to quit) 1
Wrong!
What is 4 - 0? (or q to quit) q

As mentioned in Scribbles, cell #3 above has multiple bugs. I see bugs related to quitting.


2017-09-25 Added stuff below to fix bugs. What bugs are left?


In [5]:
def math_quiz():
    operators = OrderedDict((
        ('+', add),
        ('-', subtract),
        ('*', multiply),
        ('/', divide),
    ))

    if False:
        prompt = f'Enter {", ".join(operators)}, or q to quit. '
        for operator_symbol in iter(partial(input, prompt), 'q'):
            if operator_symbol in operators:
                break
            print('try again')
        else:
            return

    prompt = f'Enter minimum integer or q to quit. '
    for s in iter(partial(input, prompt), 'q'):
        try:
            minimum = int(s)
        except ValueError:
            print('try again')
        else:
            break
    else:
        return

    prompt = f'Enter maximum integer or q to quit. '
    for s in iter(partial(input, prompt), 'q'):
        try:
            maximum = int(s)
        except ValueError:
            print('try again')
        else:
            break
    else:
        return

    minimum, maximum = sorted((minimum, maximum))

    while True:
        operator_symbol = choice(list(operators))
        x, y = (randint(minimum, maximum) for _ in range(2))
        prompt = f'What is {x} {operator_symbol} {y}? (or q to quit) '
        answer = input(prompt)
        if answer.lower()[0] == 'q':
            return
        correct_answer = operators[operator_symbol](x, y)
        if answer.strip() == str(correct_answer):
            print('Correct!')
        else:
            print('Wrong!')

In [6]:
math_quiz()


Enter minimum integer or q to quit. q

In [7]:
math_quiz()


Enter minimum integer or q to quit. -10
Enter maximum integer or q to quit. q

In [8]:
math_quiz()


Enter minimum integer or q to quit. -3
Enter maximum integer or q to quit. +7
What is 4 + -1? (or q to quit) q

In [9]:
math_quiz()


Enter minimum integer or q to quit. -8
Enter maximum integer or q to quit. +5
What is 2 - -7? (or q to quit) 5
Wrong!
What is -8 + -5? (or q to quit) -13
Correct!
What is -8 + -1? (or q to quit) -9
Correct!
What is 1 / 5? (or q to quit) 0
Correct!
What is -2 + 4? (or q to quit) q