from random import randint, choice
def add(x, y):
return x + y
def sub(x, y):
return x - y
def exam():
cmds = {‘+’: add, ‘-‘: sub}
nums = [randint(1, 100) for i in range(2)]
nums.sort(reverse=True)
op = choice(‘+-‘)
result = cmdsop
prompt = ‘%s %s %s = ’ % (nums[0], op, nums[1])
counter = 0
while counter < 3:
try:
answer = int(input(prompt))
except:
print()
continue
if answer == result:
print('Very Good!')
break
else:
print('Wrong Answer!')
counter += 1
else:
print('The Answer:')
print('%s%s' % (prompt, result))
if name == ‘main‘:
while True:
exam()
try:
yn = input(‘Continue(y/n)? ‘).strip()[0]
except IndexError:
continue
except (KeyboardInterrupt, EOFError):
yn = ‘n’
if yn in 'nN':
print('\nBye-bye')
break