Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
To help my daughters learn fractions, I made an exercise generator. Now they hate me.
from random import randrange, random
from fractions import Fraction
from functools import reduce
from operator import mul
def pf(f):
'''Pretty print the fraction'''
return "{}/{}".format(f.numerator,f.denominator)
while True:
fractions = [Fraction( randrange(1,30), randrange(1,30) ) for i in range(randrange(2,6))]
final = reduce(mul, fractions)
if final.denominator <= 30:
print("{} = {}".format(" x ".join([pf(f) for f in fractions]),pf(final)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment