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]:
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()
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()
In [7]:
math_quiz()
In [8]:
math_quiz()
In [9]:
math_quiz()