Hi I am new to python. I am trying to get my fraction program to work but I keep getting this error message: Any advice would be helpful!

File "main.py", line 5, in ?
    from fraction import*
  File "/home/students/bf133052/CS4223/fraction.py", line 10
    def__init__(self,numerator,denominator=1):
                                             ^
SyntaxError: invalid syntax

here is the main.py

#file: main.py

from fraction import*

f=fraction()

i=fraction(7,13)

c=fraction(3,30)

r=fraction()

r.set(20,45)

m=i.multipy(c)

d=i.divide(c)

print "f =", f

print "i =", i

print "c =", c

print "r =", r

print "i*c", m

print "i/c", d
# file: fraction.py
# Fraction Class to be used

class Fraction:
        "A class to use to solve fractions"

#Constructor method, creates the fraction 0/1 by default or creates the
#fraction a/b if values of a and b are supplied.

        def__init__(self,numerator,denominator=1):
                g = gcd(numerator, denominator)
                self.numerator = numerator / g
                self.denominator = denominator / g

        def__str__(self):
                return "%d / %d %(self.numerator, self.denominator)

        def multiply(self, f):
                x = fraction(self.numerator * f.numerator, self.denominator * f.denominator)
                return x

        def divide(self, f):
                d = fraction(self.numerator * f.denominator,self.denominator * f.numerator)
                return d

        def flip(self):
                "reverse the fraction"
                return fraction(self.denominator, self.numerator)

Recommended Answers

All 3 Replies

There is a space between the "def" and the function name, so it is
def __init__(self):

thank you for all your help! i got it working finally.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.