Fraction Machine w/ Python

PythonRaspberry Pi

Welcome to the Fraction Machine tutorial. This simple python code will add, subtract, multiply, or divide two fractions and provide the results as a fraction. I guess it could be called a basic fraction calculator. We started this project when my daughter Stella had some homework involving fractions and we were struggling with an easy way to check our work. Hope you enjoy it as much as we did.

Ready? Here we go! … The first thing you’ll need to do is import the fraction module and assign it an easy to remember name like frac. The fraction module has all the tools we need to process our fractions. Modules are really awesome. Modules allow us to use a set of tools that otherwise we’d have to figure out the code for and retype each time we need to use them.

from fractions import Fraction as frac

The next thing to do is create a variable to signal staying in the program to do more calculations or exit. This will be our “loop” variable. After each fraction that’s resolved the user will be given the option to begin again or exit the program. More details about how this works is below.

loop = "YES"

Next is the introduction and information gathering tasks. Below we get the users name to add some personalization. This isn’t absolutely necessary but it’s good exercise and makes the program warm and welcoming.

name = input("Hello, Welcome to the fraction machine. What's your name? ")
print("")

Here is the beginning of the loop, the real guts of our program. As long as the loop variable equals “YES” or “Y” the program will continue to run and cycle through the tasks.

while loop == "YES" or loop == "Y":

Now we gather our fractions, we define how they should be entered and assign them to the firstnum and secondnum variables.

firstnum = input("Welcome " + name + ", What is your first fraction? (example: 1/2) ")
print("")
secondnum = input("and what is the second fraction? ")
print("")

Here we determine what type of math should be performed on our fractions and save that choice to the function variable. Notice we use the .upper() function to make all of our choices uppercase for easier evaluation later. It’s important to note that when evaluating variables that an “M” is not equal to an “m”, variables are case sensitive so implementing the .upper() function normalizes the input so it can be evaluated.

function = input("What do you want to do? M, D, A, S ")
function = function.upper()
print("")

Using If we evaluate the function variable and if the selection matches we perform the calculation. If the user entered M for multiplication we use the fraction library on each fraction entered and multiply the results. We return the answer and then we ask the user if they want to perform another calculation. We then perform the same routine for each option given, Multiply, Divide, Add, or Subtract.

if function == "M":
    answer = str(frac(firstnum) * frac(secondnum))
    print ("The answer is " + answer)
    print("")
    loop = input("Again? yes/no ")
    print("")
    loop = loop.upper()

elif function == "D":
    answer = str(frac(firstnum) / frac(secondnum))
    print ("The answer is " + answer)
    print("")
    loop = input("Again? yes/no ")
    print("")
    loop = loop.upper()

elif function == "A":
    answer = str(frac(firstnum) + frac(secondnum))
    print ("The answer is " + answer)
    print("")
    loop = input("Again? yes/no ")
    print("")
    loop = loop.upper()

elif function == "S":
    answer = str(frac(firstnum) - frac(secondnum))
    print ("The answer is " + answer)
    print("")
    loop = input("Again? yes/no ")
    print("")
    loop = loop.upper()

If the user enters something other than M, D, A, or S the WOMP WOMP error message is generated and the user is asked to try again.

else:
    print("WOMP WOMP, that is an invalid math function. Lets try that again")
    print("")

and finally if you’d like to run the code yourself here is a link to the GitHub code repository

Leave a Reply

Your email address will not be published. Required fields are marked *