Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help writing 2 functions in python below are instructions: You will be responsible for creating 2 functions in your main file that utilize the

Need help writing 2 functions in python below are instructions:

You will be responsible for creating 2 functions in your main file that utilize the fraction class. ConvertLine function also uses two other functions that i have given you in the file.

def getFile(): ''' Returns as a tuple the file name entered by the user and the open file object. If the file exceeds three attempts of opening sucessfully, an IO exception is raised. '''

def convertLine(line, factor): ''' If line begins with a digit, then returns line with the value incremented by factor. Otherwise, returns line unaltered. (For example, for a factor of 2, '1/4 cup sugar' returns as '1/2 cup sugar and '2 cups sugar' returns as '4 cups sugar'.) '''

And below is my current getFile():

def getFile(): import os.system() ''' Returns as a tuple the file name entered by the user and the open file object. If the file exceeds three attempts of opening sucessfully, an IO exception is raised. ''' sum = 0 input_file= None file_name = None # file_open=False # while os.path.isfile(file_name) < 3 and file_open==False: for i in range(3): try: file_name = input("Input a file name: ") input_file = open(file_name, 'r') break except IOError: sum = sum + 1 if sum == 3: raise IOError("You have entered an valid file name over 3 times.") return (file_name, input_file)

Below is the current line:

def convertLine(line, factor): ''' If line begins with a digit, then returns line with the value incremented by factor. Otherwise, returns line unaltered. (For example, for a factor of 2, '1/4 cup sugar' returns as '1/2 cup sugar and '2 cups sugar' returns as '4 cups sugar'.) ''' # code goes here # The idea is that you are going to step through the line of the recipe # if you find the digit in the line # scan through the line looking for the fraction and mulitply it by the factor to create a new fraction # then you need to build the new line by removing the original measure and putting the new fraciton measure in the line # return the new line # this method should utilize the scanasfraction and removemeasure functions return conv_line

And below is the imported fraction class

# Fraction Class # Do not change anything in this file class Fraction: def __init__(self, numerator, denominator): self.__numerator = numerator self.__denominator = denominator def __str__(self): return str(self.__numerator) + '/' + str(self.__denominator) def getNumerator(self): return self.__numerator def getDenominator(self): return self.__denominator def get(self): return (self.getNumerator(), self.getDenominator()) def setNumerator(self, value): self.__numerator = value def setDenominator(self, value): if value == 0: raise ValueError('Divide by Zero Error') self.__denominator = value def set(self, numer, denom): self.setNumerator(numer) self.setDenominator(denom) def reduce(self): gcd = self.__calcGCD() self.__numerator = self.__numerator // gcd self.__denominator = self.__denominator // gcd def __calcGCD(self): a = max(self.__numerator, self.__denominator) b = min(self.__numerator, self.__denominator) while b != 0: temp = b b = a % b a = temp return a def __adjust(self, factor): self.setNumerator(self.getNumerator() * factor) self.setDenominator(self.getDenominator() * factor) def copy(self): new_frac = Fraction(self.__numerator, self.__denominator) return new_frac def __neg__(self): return Fraction(-self.__numerator, self.__denominator) def __add__(self, rfraction): numer = self.__numerator * rfraction.getDenominator() + \ rfraction.getNumerator() * self.__denominator denom = self.__denominator * rfraction.getDenominator() resultFrac = Fraction(numer, denom) resultFrac.reduce() return resultFrac def __sub__(self, rfraction): return self + (-rfraction) def __mul__(self, rfraction): numer = self.__numerator * rfraction.getNumerator() denom = self.__denominator * rfraction.getDenominator() resultFrac = Fraction(numer, denom) resultFrac.reduce() return resultFrac def __eq__(self, rfraction): temp_frac1 = self.copy() temp_frac2 = rfraction.copy() temp_frac1.reduce() temp_frac2.reduce() if temp_frac1.getNumerator() == temp_frac2.getNumerator() and \ temp_frac1.getDenominator() == temp_frac2.getDenominator(): return True else: return False def __neq__(self, rfraction): return not self.__eq__(rfraction) def __lt__(self, rfraction): lessthan = False temp_frac1 = self.copy() temp_frac2 = rfraction.copy() temp_frac1.__adjust(temp_frac2.getDenominator()) temp_frac2.__adjust(temp_frac1.getDenominator()) if temp_frac1.getNumerator() < temp_frac2.getNumerator(): lessthan = True return lessthan def __le__(self, rfraction): if self == rfraction or self < rfraction: return True else: return False def __gt__(self, rfraction): if not(self <= rfraction): return True else: return False def __ge__(self, rfraction): if not(self < rfraction): return True else: return False 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Relational Model A Retrospective Review And Analysis

Authors: C. J. Date

1st Edition

0201612941, 978-0201612943

More Books

Students also viewed these Databases questions