-Help Needed- Fraction solver program (Python)

Hello all,

I am struggling to write a fraction solver program in Python.  This program needs to complete the following simple tasks: 1.) Given two fractions as tuples, multiply them. 2.) Given two fractions as tuples, divide them. and 3.) Give a list of fractions (as tuples) return the smallest value.

I am struggling to understand how tuple's, and how to perform the multiplication and division functions given the user input.

For example, here is what I currently have (very little):


import math

import functools

import string

def main():
      while True:

            fraction = input("Enter a fraction: ")

def frac_multiply(x,y):

      numerator1, denominator1 = x
      numerator2, denominator2 = y

      return numerator, denominator

if __name__ == '__main__':

Basically, the program should look like this when it runs:

Multiplication and Division:

Enter a fraction: 5/3

Enter a fraction: 10/3

Multiplication equals: 50/9

Division equals (the 1st by the 2nd)" 5/10

Smallest fraction:

Enter a fraction: 1/3

Enter a fraction: 10/3

Enter a fraction: 6/4

Smallest fraction: 1/3

I am stumped on how to perform any of the calculations when the user inputs n/d (numerator slash denominator)

Any help would be most appreciated.

Well it's pretty easy what you are asking but i think you need to do some reserch yoursef in order to become a better programmer. Me solving this for you will not actually help you since you will not learn anything by asking people to do it for you.

If you want my help i can give you a simple step by step on how to solve your problem.

Here is the fist step in solving your problem :

1. Separate your script acording to your requirements. ( In other words how would you separate your script in smaller steps that you can implement in simple functions)

 1.a Create a function that can take input from the user

 1.b Create a function that based on that user input can tell you what operation needs to done( multiply ,devide, sort)

 1.c Create a function that returns the required result. 


Here is your solution , but beware that this is not the right solution since you have to do it using tuples

If you copy paste this code your techer will know you you didn't do it yourself


import re
from fractions import Fraction

# Splits the fraction taken from the command line into numerator and denominator
# and converts it into integers
def split_fraction(string_from_input):

splited_fraction = re.split('/', string_from_input)

numerator, denominator = int(splited_fraction[0]), int(splited_fraction[1])

return numerator, denominator

# This function devides 2 fractions and returns the result
# (regular division done with / always retuns a float)
def devide_fractions(num1,denum1, num2, denum2):

top = num1 * denum2
bottom = denum1 * num2
return top,bottom

# infinite loop so we can try more than once.
while 1:
    # Store the input into variables that we can work with
    fraction_string1 = input("Enter first fraction:")
    fraction_string2 = input("Enter second fraction:")

    # Transform the input given by the user into ints
    numerator1, denominator1 = split_fraction(fraction_string1)
    numerator2, denominator2 = split_fraction(fraction_string2)

    # Store the multiplyed result into a variable that we can use later
    multiplied = Fraction(numerator1,denominator1) * Fraction(numerator2, denominator2)

    # using pretty funtion devide the 2 fractions given by the user
    prety_function = devide_fractions(numerator1, denominator1, numerator2, denominator2)

    # Store the result of the division above in a variable
    devided = Fraction(prety_function[0],prety_function[1])

    # Store the fist and second fraction given by the user as Fraction objects so we can compare
    # them later
    first_fraction = Fraction(numerator1,denominator1)
    second_fraction = Fraction(numerator2,denominator2)

    # make a list that stores all the fractions we have so we can reurn the smallest one
    list_of_fractions = [first_fraction, second_fraction , multiplied , devided]

    print("Multiplication equals:",multiplied , '\n')
    print("Division equals:", devided , '\n')
    print("Smallest fraction:", min(list_of_fractions) , '\n')

    exit_program = input("Would you like to exit ? (y / n) : ")

    if exit_program == "y" or exit_program == "Y":
        print('\n ----------------------------------------')

I am not asking you to solve it for me, just a few pointers.  I won't learn anything if it's just done for me.  Thanks for the help.