#### Before you keep reading...

Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

#### Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

Error

SyntaxError: Missing parentheses in call to ‘print’. Did you mean print(…)? (line 18). Docstrings cannot be processed.

class Fraction:
def __init__(self,top,bottom):
self.num = top #the numerator is on top
self.den = bottom #the denominator is on the bottom
def __repr__(self):
if self.num > self.den:
retWhole = self.num / self.den
retNum = self.num - (retWhole * self.den)
return str(retWhole) + " " + str(retNum)+"/"+str(self.den)
else:
return str(self.num)+"/"+str(self.den)
def show(self):
print self.num,"/",self.den
def __add__(self,otherfraction):

otherfraction = self.toFract(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 __radd__(self,leftNum):
otherfraction = self.toFract(leftNum)
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 __cmp__(self,otherfraction):
num1 = self.num*otherfraction.den
num2 = self.den*otherfraction.num
if num1 < num2:
return -1
else:
if num1 == num2:
return 0
else:
return 1
def toFract(self,n):
if isinstance(n,int):
otherfraction = Fraction(n,1)
elif isinstance(n, float):
wholePart = int(n)
fracPart = n - wholePart

fracNum = int(fracPart * 100)
newNum = wholePart * 100 + fracNum
otherfraction = Fraction(newNum,100)
elif isinstance(n,Fraction):
otherfraction = n
else:
print "Error: cannot add a fraction to a ", type(n)
return None
return otherfraction
#gcd is a helper function for Fraction
def gcd(m,n):
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n

You have attempted

of

activities on this page