In: Computer Science
At the moment this code only give you results for whatever fraction you put for "x", and "y" inside the main code. How do I get this existing code to ask the user to enter a fraction, and then ask another questions for another fraction, and then display the results of adding, multiplying, and dividing the fractions?
Something like:
"Please enter the first fraction: " >>> 1/2
"Please enter the second fraction:" >>> 5/3
Result of adding = x/x
Result of multiplying = x/x
Result for dividing = x/x
*** DO NOT CHANGE THE CODE TOO MUCH. MAIN GOAL IS TO ONLY MAKE IT ASK THE USER FOR INPUT ***
def gcd(m,n):
    while m%n != 0:
        oldm = m
        oldn = n
        m = oldn
        n = oldm%oldn
return n
class Fraction:
    def __init__(self,top,bottom):
        self.num = top
        self.den = bottom
    def __str__(self): #We cannot print out
fractions because python does not know how to convert an object
into a string
return str(self.num)+"/"+str(self.den)
    def __add__(self,otherfraction):
        newnum =
self.num*otherfraction.den + \
                    
self.den*otherfraction.num
        newden = self.den *
otherfraction.den
        common =
gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
    def __eq__(self, other):
        firstnum = self.num *
other.den
        secondnum = other.num *
self.den
      
        return firstnum ==
secondnum
    def __mul__(self,other):
        newnum =
self.num*other.num
        newden =
self.den*other.den
       
common=gcd(newnum,newden)
return Fraction(newnum//common, newden//common)
    def __truediv__ (self, other):
        newnum =
self.num*other.den
        newden =
self.den*other.num
       
common=gcd(newnum,newden)
        return
Fraction(newnum//common, newden//common)
                      
def main():
    x = Fraction(10,7)
    y = Fraction(5,9)
    print(x+y)
    print(x*y)
    print(x/y)
  
main()
  

def gcd(m,n):
    while m%n != 0:
        oldm = m
        oldn = n
        m = oldn
        n = oldm%oldn
    return n
class Fraction:
    def __init__(self,top,bottom):
        self.num = top
        self.den = bottom
    def __str__(self): #We cannot print out fractions because python does not know how to convert an object into a string
        return str(self.num)+"/"+str(self.den)
    def __add__(self,otherfraction):
        newnum = self.num*otherfraction.den + \
                     self.den*otherfraction.num
        newden = self.den * otherfraction.den
        common = gcd(newnum,newden)
        return Fraction(newnum//common,newden//common)
    def __eq__(self, other):
        firstnum = self.num * other.den
        secondnum = other.num * self.den
      
        return firstnum == secondnum
    def __mul__(self,other):
        newnum = self.num*other.num
        newden = self.den*other.den
        common=gcd(newnum,newden)
        return Fraction(newnum//common, newden//common)
    def __truediv__ (self, other):
        newnum = self.num*other.den
        newden = self.den*other.num
        common=gcd(newnum,newden)
        return Fraction(newnum//common, newden//common)
def readFraction():
        line = input('Please enter a fraction: ')
        x, y = line.split('/')
        x, y = int(x), int(y)
        return Fraction(x, y)
def main():
    x = readFraction()
    y = readFraction()
    print('Result of adding = ', x+y)
    print('Result of multiplying = ', x*y)
    print('Result of dividing = ', x/y)
  
main()
************************************************** You would have to parse user input and create a fraction object.. like i did in readFraction method. Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.