Tackling Fractions With Python

Python function to run call on other functions depending on user input

Fractions, not something I’ve really looked at since my school days. However with home schooling currently taking place here in the UK due to another Coronavirus lockdown, it is something that I’ve been refreshing my knowledge on this week.

Note: https://www.calculatorsoup.com/calculators/math/fractions.php provides an online calculator to help with fractions. The site also provides the formulas to help with fraction mathematics, which I’m also using in this Python program.

Whilst relearning about fractions I decided to create a Python program so that I could double check my learnings and get a better understanding of the formulas involved. The program needs some work as I repeat a few lines in each function and I will be looking to change my simplification method so that it does more than try dividing by 2.

# geektechstuff fractions

# Requires Python3.6 or above for f’ string usage

def main():

    print(“Welcome to GeekTechStuff Fractions Calculator”)

    print(” 1 = Addition \n 2 = Subtraction \n 3 = Multiplication \n 4 = Division”)

    calc_mode = input (“Which mode would you like to use: “)

    # try / except to catch any non-intergers or numbers other than 1 to 4

    try:

        calc_mode = int(calc_mode)

        if (calc_mode > 0 and calc_mode < 5):

            print(f”You chose {calc_mode}”)

            # each calc_mode calls on a function below

            if calc_mode==1:

                return(addition())

            if calc_mode==2:

                return(subtract())

            if calc_mode==3:

                return(multiply())

            if calc_mode==4:

                return(divide())

            else:

                print(f”Sorry I did not understand {calc_mode}”)

                return()

        else:

            print(f”Sorry I did not understand {calc_mode}”)

            return()

    except:

        print(f’You choice of {calc_mode}, caused an error. Is {calc_mode} a whole number in the range of 1 to 4?’)

        return ()

def addition():

    print(“Enter your fraction as: a over b and c over d, e.g. a/b + c/d”)

    print(“Each variable should be a whole number.”)

    a = input(“Value for a is: “)

    b = input(“Value for b is: “)

    c = input(“Value for c is: “)

    d = input(“Value for d is: “)

    try:

        a = int(a)

        b = int(b)

        c = int(c)

        d = int(d)

        value_top_left = a*d

        value_top_right = b*c

        value_bottom = b*d

        print(f”The equation orginally looked like {a}/{b} + {c}/{d}, it now looks like ({value_top_left} + {value_top_right}) / {value_bottom}”)

        value_top = value_top_left + value_top_right

        print(f”The equation then becomes {value_top}/{value_bottom}”)

        if ((value_top % 2 == 0) and (value_bottom % 2 == 0)):

            value_top_divide_by_2 = value_top/2

            value_top = int(value_top_divide_by_2)

            value_bottom_divide_by_2 = value_bottom/2

            value_bottom = int(value_bottom_divide_by_2)

            print(f”The equation can be simplified to: {value_top}/{value_bottom}’s”)

        else:

            print(f”Sorry I could not simplify this equation”)

        fraction_to_whole(value_top,value_bottom)

    except:

        print(f’Are {a},{b},{c},{d} all whole numbers?’)

def divide():

    print(“Enter your fraction as: a over b and c over d, e.g. a/b / c/d”)

    print(“Each variable should be a whole number.”)

    a = input(“Value for a is: “)

    b = input(“Value for b is: “)

    c = input(“Value for c is: “)

    d = input(“Value for d is: “)

    try:

        a = int(a)

        b = int(b)

        c = int(c)

        d = int(d)

        value_top = a*d

        value_bottom = b*c

        print(f”The equation orginally looked like {a}/{b} / {c}/{d}, it now looks like {value_top} / {value_bottom}”)

        if ((value_top % 2 == 0) and (value_bottom % 2 == 0)):

            value_top_divide_by_2 = value_top/2

            value_top = int(value_top_divide_by_2)

            value_bottom_divide_by_2 = value_bottom/2

            value_bottom = int(value_bottom_divide_by_2)

            print(f”The equation can be simplified to: {value_top}/{value_bottom}’s”)

        else:

            print(f”Sorry I could not simplify this equation”)

        fraction_to_whole(value_top,value_bottom)

    except:

        print(f’Are {a},{b},{c},{d} all whole numbers?’)

def multiply():

    print(“Enter your fraction as: a over b and c over d, e.g. a/b x c/d”)

    print(“Each variable should be a whole number.”)

    a = input(“Value for a is: “)

    b = input(“Value for b is: “)

    c = input(“Value for c is: “)

    d = input(“Value for d is: “)

    try:

        a = int(a)

        b = int(b)

        c = int(c)

        d = int(d)

        value_top = a*c

        value_bottom = b*d

        print(f”The equation orginally looked like {a}/{b} x {c}/{d}, it now looks like {value_top} / {value_bottom}”)

        if ((value_top % 2 == 0) and (value_bottom % 2 == 0)):

            value_top_divide_by_2 = value_top/2

            value_top = int(value_top_divide_by_2)

            value_bottom_divide_by_2 = value_bottom/2

            value_bottom = int(value_bottom_divide_by_2)

            print(f”The equation can be simplified to: {value_top}/{value_bottom}’s”)

        else:

            print(f”Sorry I could not simplify this equation”)

        fraction_to_whole(value_top,value_bottom)

    except:

        print(f’Are {a},{b},{c},{d} all whole numbers?’)

def subtract():

    print(“Enter your fraction as: a over b and c over d, e.g. a/b – c/d”)

    print(“Each variable should be a whole number.”)

    a = input(“Value for a is: “)

    b = input(“Value for b is: “)

    c = input(“Value for c is: “)

    d = input(“Value for d is: “)

    try:

        a = int(a)

        b = int(b)

        c = int(c)

        d = int(d)

        value_top_left = a*d

        value_top_right = b*c

        value_bottom = b*d

        print(f”The equation orginally looked like {a}/{b} – {c}/{d}, it now looks like ({value_top_left} – {value_top_right}) / {value_bottom}”)

        value_top = value_top_left – value_top_right

        print(f”The equation then becomes {value_top}/{value_bottom}”)

        if ((value_top % 2 == 0) and (value_bottom % 2 == 0)):

            value_top_divide_by_2 = value_top/2

            value_top = int(value_top_divide_by_2)

            value_bottom_divide_by_2 = value_bottom/2

            value_bottom = int(value_bottom_divide_by_2)

            print(f”The equation can be simplified to: {value_top}/{value_bottom}’s”)

        else:

            print(f”Sorry I could not simplify this equation”)

        fraction_to_whole(value_top,value_bottom)

    except:

        print(f’Are {a},{b},{c},{d} all whole numbers?’)

def fraction_to_whole(fraction_part_a, fraction_part_b):

    if fraction_part_a > fraction_part_b:

        divided = fraction_part_a / fraction_part_b

        divided = int(divided)

        modulus = fraction_part_a % fraction_part_b

        print(f”And simplified again to {divided} and {modulus}/{fraction_part_b}’s”)

    else:

        print(f”I believe the answer is {fraction_part_a}/{fraction_part_b}”)

main()

If you want to know more about fractions then please check out the BBC Bitesize resources available at:

Keystage 1 – https://www.bbc.co.uk/bitesize/topics/z3rbg82

Keystage 2 – https://www.bbc.co.uk/bitesize/topics/zhdwxnb

Keystage 3 – https://www.bbc.co.uk/bitesize/guides/zt6p34j/revision/1

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.