#!/usr/bin/env python # coding: utf-8 # These are some experiments for refactoring [kraftur](https://github.com/kraftur)'s # [math-game](https://github.com/kraftur/math-game). # In[1]: addition_problems_and_answers = [ ('11 + 11', '22'), ('11 + 2', '13'), ('11 + 3', '14'), ] good = addition_problems_and_answers good # 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 # In[3]: from random import choice, choices # In[4]: for _ in range(10): print(choice(b)) # In[5]: choices(b, k=10) # In[6]: print('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 # In[8]: a.split('\n') # In[9]: a.strip().split('\n') # 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 # 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}. ') # In[13]: choices = ' or '.join(map(lambda action: f'"{action}"', actions)) print(f'Please type {choices}. ')