Question
I have some problems with writing codes to design a fraction calculator with python. Here are my code. class Fraction: def __init__(self, num, denom): self.num
I have some problems with writing codes to design a fraction calculator with python. Here are my code. class Fraction: def __init__(self, num, denom): self.num = num self.denom = denom if denom < 0: raise ValueError("The denominator can't be 0") def __str__(self): return str(self.num) + '/' + str(self.denom) def plus(self, other): return Fraction(self.num * other.denom + self.denom * other.num, self.denom * other.denom) def minus(self, other): return Fraction(self.num * other.denom - self.denom * other.num, self.denom * other.denom) def times(self, other): """ Multiply two fractions using simplest approach and return a new Fraction """ return Fraction(self.num * other.num, self.denom * other.denom) def divide(self, other): """ Add two fractions using simplest approach. Division is just invert and multiply and return a new Fraction """ return Fraction(self.num * other.denom, self.denom * other.num) def equal(self, other): """ return True/False if the two fractions are equivalent """ a = Fraction(self.num, self.denom) b = Fraction(other.num, other.denom) print(a == b) def tests(): """ We'll see a better testing approach next week but here's a start. Note that each statement includes the result of the computation plus the expected answer to help to quickly identify if everything works properly. """ f12 = Fraction(1, 2) f44 = Fraction(4, 4) print(f12, '+', f12, '=', f12.plus(f12), '[4/4]') print(f44, '-', f12, '=', f44.minus(f12), '[4/8]') def get_number(prompt): """ read and return an integer from the user. """ while True: inp = input(prompt) try: return float(inp) except ValueError: print('Error:', inp, 'is not a number. Please try again...') def main(): """ Fraction calculations """ print('Welcome to the fraction calculator!') num1 = get_number("Fraction 1 numerator: ") denom1 = get_number("Fraction 1 denominator: ") operator = input("Operation (+, -, *, /): ") num2 = get_number("Fraction 2 numerator: ") denom2 = get_number("Fraction 2 denominator: ") result = None f1 = Fraction(num1, denom1) f2 = Fraction(num2, denom2) # apply f1 operator f2 and print the result if operator == '+': print(f1, '+', f2, '=' + str(f1.plus(f2))) elif operator == '-': print(f1, '-', f2, '=' + str(f1.minus(f2))) elif operator == '*': print(f1, '*', f2, '=' + str(f1.times(f2))) elif operator == '/': print(f1, '/', f2, '=' + str(f1.divide(f2))) print(f1, '==', f1.equal(f2)) if __name__ == '__main__': tests() main() I want the program to raise a ZeroDivisionError when I enter the wrong denominator number and ask the user to input again
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started