addition_problems_and_answers = [
('11 + 11', '22'),
('11 + 2', '13'),
('11 + 3', '14'),
]
good = addition_problems_and_answers
good
[('11 + 11', '22'), ('11 + 2', '13'), ('11 + 3', '14')]
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
[('11 + 11', '22'), ('11 + 2', '13'), ('11 + 3', '14')]
from random import choice, choices
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')
choices(b, k=10)
[('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')]
print('hello', 'world')
hello world
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
[('11', '11'), ('11', '2'), ('11', '3')]
a.split('\n')
['', ' 11 11', ' 11 2', ' 11 3', '']
a.strip().split('\n')
['11 11', ' 11 2', ' 11 3']
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
[(11, 11), (11, 2), (11, 3)]
actions = {
'add': 1,
'subtract': 'hello',
'stats': int,
'quit': 1j,
}
choices = ' or '.join(f'"{action}"' for action in actions)
print(f'Please type {choices}. ')
Please type "add" or "subtract" or "stats" or "quit".
choices = ' or '.join(map(lambda action: f'"{action}"', actions))
print(f'Please type {choices}. ')
Please type "add" or "subtract" or "stats" or "quit".