#!/usr/bin/python3 '''Some arithmetic practice for my son''' from __future__ import print_function import sys # import math import random def usage(retval): '''Output a usage message''' sys.stderr.write('%s --problems n\n' % sys.argv[0]) sys.exit(retval) def addition(): '''addition''' correct = incorrect = 0 left = random.randrange(-10, 10) right = random.randrange(-10, 10) sys.stdout.write('%d + %d = ' % (left, right)) sys.stdout.flush() line = input() # pylint: disable=bad-builtin if int(line) == left + right: print('Good, that is correct') correct = 1 else: print('Sorry, %d + %d is %d' % (left, right, left + right)) incorrect = 1 return correct, incorrect def subtraction(): '''subtraction''' correct = incorrect = 0 left = random.randrange(-10, 10) right = random.randrange(-10, 10) if left < right: left, right = right, left sys.stdout.write('%d - %d = ' % (left, right)) sys.stdout.flush() line = input() # pylint: disable=bad-builtin if int(line) == left - right: print('Good, that is correct') correct = 1 else: print('Sorry, %d - %d is %d' % (left, right, left - right)) incorrect = 1 return correct, incorrect def multiplication(): '''multiplication''' correct = incorrect = 0 left = random.randrange(-10, 10) right = random.randrange(-10, 10) sys.stdout.write('%d * %d = ' % (left, right)) sys.stdout.flush() line = input() # pylint: disable=bad-builtin if int(line) == left * right: print('Good, that is correct') correct = 1 else: print('Sorry, %d * %d is %d' % (left, right, left * right)) incorrect = 1 return correct, incorrect # I thought this was how it "should" work, but it's not! #def my_divmod(left, right): # '''Do a divmod with an elementary-school math definition of how things work''' # left_sign = math.copysign(1, left) # right_sign = math.copysign(1, right) # # left = int(math.fabs(left)) # right = int(math.fabs(right)) # # quotient, remainder = divmod(left, right) # # if left_sign == right_sign: # return quotient, remainder # else: # return -quotient, remainder def my_divmod(left, right): '''Return a python divmod''' return divmod(left, right) def division(): '''division''' correct = incorrect = 0 left = random.randrange(-10, 10) right = random.randrange(-10, 10) (expected_quotient, expected_remainder) = my_divmod(left, right) sys.stdout.write('%d / %d = quotient, remainder: ' % (left, right)) sys.stdout.flush() line = input() # pylint: disable=bad-builtin fields = line.split(',') fields = [string.strip() for string in fields] user_quotient = int(fields[0]) if fields[1:]: user_remainder = int(fields[1]) else: print('Inferring that the remainder is 0') user_remainder = 0 if user_quotient == expected_quotient: if user_remainder == expected_remainder: print('Quotient and remainder are both correct') correct = 1 else: print('Sorry, the quotient is correct, but the remainder should have been', expected_remainder) incorrect = 1 else: if user_remainder == expected_remainder: print('Sorry, the remainder is correct, but the quotient should have been', expected_quotient) incorrect += 1 else: print('Sorry, the quotient and remainder should have been {}, {}'.format(expected_quotient, expected_remainder)) incorrect += 1 return correct, incorrect def main(): '''Main function''' problems = None while sys.argv[1:]: if sys.argv[1] == '--problems': problems = int(sys.argv[2]) del sys.argv[1] elif sys.argv[1] in ['-h', '--help']: usage(0) else: sys.stderr.write('%s: Unrecognized option: %s\n' % (sys.argv[0], sys.argv[1])) usage(1) del sys.argv[1] if problems is None: sys.stderr.write('%s: --problems ia a required option\n' % sys.argv[0]) usage(0) correct = incorrect = 0 for problemno in range(problems): print('Problem %d of %d' % (problemno + 1, problems)) operator = random.randrange(4) if operator == 0: correct_delta, incorrect_delta = addition() elif operator == 1: correct_delta, incorrect_delta = subtraction() elif operator == 2: correct_delta, incorrect_delta = multiplication() elif operator == 3: correct_delta, incorrect_delta = division() else: sys.stderr.write('%s: Internal error: Operator not 0..4\n' % sys.argv[0]) sys.exit(1) correct += correct_delta incorrect += incorrect_delta sys.stdout.write('\n') assert correct + incorrect == problems print('You got %d right and %d wrong, for a percentage of %f' % ( correct, incorrect, 100.0 * float(correct) / (correct + incorrect), )) main()