Question

In: Computer Science

At the moment this code only give you results for whatever fraction you put for "x",...

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()


  

Solutions

Expert Solution

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.


Related Solutions

redo Fundamental Theorem of Arithmetic for F[x], F=field, including whatever preliminary results.
redo Fundamental Theorem of Arithmetic for F[x], F=field, including whatever preliminary results.
MATLAB ONLY please. Please put the entire code below. 1. you will read a list of...
MATLAB ONLY please. Please put the entire code below. 1. you will read a list of internet logs from a notepad. 2. then you will extract the following. - a host (e.g., '146.204.224.152') - a user_name (e.g., 'feest6811' note: sometimes the username is missing! In this case, use '-' as the value for the username.) - the timme a request was made (e.g., '21/Jun/2019:15:45:24-0700') - the post request type (e.g., 'POST /incentivize HTTP/1.1' note: not everthing is a POST!) Your...
After doing a chest x-ray, your doctor says the results are inconclusive. So, you are put...
After doing a chest x-ray, your doctor says the results are inconclusive. So, you are put on a six-month course of isoniazid to be safe. About six months later, while sitting in your medical microbiology class during a lecture on tuberculosis, you suddenly realize why you had that positive reaction to the skin test six months earlier. It had nothing to do with being infected, but was because you were born in Norway and your family moved to the United...
After doing a chest x-ray, your doctor says the results are inconclusive. So, you are put...
After doing a chest x-ray, your doctor says the results are inconclusive. So, you are put on a six-month course of isoniazid to be safe. About six months later, while sitting in your medical microbiology class during a lecture on tuberculosis, you suddenly realize why you had that positive reaction to the skin test six months earlier. It had nothing to do with being infected, but was because you were born in Norway and your family moved to the United...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME IDE*************** Write a recursive Racket function "keep-short-rec" that takes an integer and a list of strings as parameters and evaluates to a list of strings. The resulting list should be all of the strings from the original list, maintaining their relative order, whose string length is less than the integer parameter. For example, (keep-short-rec 3 '("abc" "ab" "a")) should evaluate to '("ab" "a") because...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME...
*****************PLEASE GIVE THE CODE IN RACKET PROGRAMMING ONLY AND MAKE SURE THE CODE RUNS ON WESCHEME IDE*************** Write a recursive Racket function "sum-diff" that takes two lists of integers that are the same length and evaluates to an integer. The resulting integer should be the sum of the absolute value of the differences between each pair of integers with the same index in the two lists. For example (sum-diff '(-1 -2 -3) '(1 2 3)) should evaluate to 12 because...
Write a Python 3 code that takes an irrational number 'x' and return 'm' fraction approximations....
Write a Python 3 code that takes an irrational number 'x' and return 'm' fraction approximations. For example let x=pi and m=4, then the program should return 4 examples of pi written as a fraction approximation.  
Suppose you have some money to invest-for simplicity, $1-and are planning to put a fraction w...
Suppose you have some money to invest-for simplicity, $1-and are planning to put a fraction w into a stock market mutual fund and the rest, (1-w), into a bond mutual fund. Suppose that a $1 invested in a stock fund yields Rs, after one year and a $1 invested in a bond fund yields Rb. Rs and Rb are random variables with expected value of 10% and 8% respectively, and standard deviation of 4% and 2% respectively. The correlation between...
Suppose you have some money to invest-for simplicity, $1-and are planning to put a fraction w...
Suppose you have some money to invest-for simplicity, $1-and are planning to put a fraction w into a stock market mutual fund and the rest ( 1− w ), into a bond mutual fund. Suppose that a $1 invested in a stock fund yields ??after one year and a $1 invested in a bond fund yields Rb . ?? and Rb are random variables with expected value of 9% and 7% respectively, and standard deviation of 4% and 2% respectively....
For a fraction nonconforming of p = 0.05, which type of plan would give you the...
For a fraction nonconforming of p = 0.05, which type of plan would give you the lowest ASN? a. Single Sampling Plans b. Double Sampling Plans c. Sequential Sampling Plans d. Simple Random Sampling Plans
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT