These are some experiments for refactoring kraftur's math-game.


In [1]:
addition_problems_and_answers = [
    ('11 + 11', '22'),
    ('11 + 2', '13'),
    ('11 + 3', '14'),
]
good = addition_problems_and_answers
good


Out[1]:
[('11 + 11', '22'), ('11 + 2', '13'), ('11 + 3', '14')]

In [2]:
addition_problems_and_answers = '''
    11 + 11, 22
    11 + 2, 13
    11 + 3, 14
    '''
a = addition_problems_and_answers
b = [
    tuple(term.strip() for term in line.split(','))
    for line in a.split('\n')
    if line.strip()
]
b


Out[2]:
[('11 + 11', '22'), ('11 + 2', '13'), ('11 + 3', '14')]

In [3]:
from random import choice, choices

In [4]:
for _ in range(10):
    print(choice(b))


('11 + 11', '22')
('11 + 2', '13')
('11 + 11', '22')
('11 + 2', '13')
('11 + 11', '22')
('11 + 2', '13')
('11 + 2', '13')
('11 + 3', '14')
('11 + 3', '14')
('11 + 2', '13')

In [5]:
choices(b, k=10)


Out[5]:
[('11 + 2', '13'),
 ('11 + 2', '13'),
 ('11 + 11', '22'),
 ('11 + 3', '14'),
 ('11 + 2', '13'),
 ('11 + 11', '22'),
 ('11 + 11', '22'),
 ('11 + 2', '13'),
 ('11 + 2', '13'),
 ('11 + 2', '13')]

In [6]:
print('hello', 'world')


hello world

In [7]:
addition_problems_text = '''
    11 11
    11 2
    11 3
'''
a = addition_problems_text
b = [
    tuple(operand.strip() for operand in line.split())
    for line in a.split('\n')
    if line.strip()
]
b


Out[7]:
[('11', '11'), ('11', '2'), ('11', '3')]

In [8]:
a.split('\n')


Out[8]:
['', '    11 11', '    11 2', '    11 3', '']

In [9]:
a.strip().split('\n')


Out[9]:
['11 11', '    11 2', '    11 3']

In [10]:
addition_problems_text = '''
    11 11
    11 2
    11 3
'''
a = addition_problems_text
b = [
    tuple(int(operand.strip()) for operand in line.split())
    for line in a.split('\n')
    if line.strip()
]
b


Out[10]:
[(11, 11), (11, 2), (11, 3)]

In [11]:
actions = {
    'add': 1,
    'subtract': 'hello',
    'stats': int,
    'quit': 1j,
}

In [12]:
choices = ' or '.join(f'"{action}"' for action in actions)
print(f'Please type {choices}. ')


Please type "add" or "subtract" or "stats" or "quit". 

In [13]:
choices = ' or '.join(map(lambda action: f'"{action}"', actions))
print(f'Please type {choices}. ')


Please type "add" or "subtract" or "stats" or "quit".