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

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

Dani AI

Generated

Good catch on the traceback: the caret is landing at your constructor because Python expects whitespace after def. As noted, it should be def __init__. You will also hit a few more issues right after fixing that: __str__ has a missing quote and % placement, your class is named Fraction but you instantiate fraction(...), multipy is misspelled in main.py, and there is no set method on the class. Finally, you call gcd(...) without defining or importing it.

A minimal skeleton that compiles and behaves as intended could look like this (note the defaults so Fraction() works, basic validation, and consistent class naming):

def gcd(a, b):
    while b:
        a, b = b, a % b
    return abs(a)

class Fraction(object):
    def __init__(self, numerator=0, denominator=1):
        if denominator == 0:
            raise ValueError("denominator cannot be 0")
        g = gcd(numerator, denominator)
        n, d = numerator // g, denominator // g
        if d < 0:  # keep sign on numerator
            n, d = -n, -d
        self.numerator, self.denominator = n, d

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

    def multiply(self, other):
        return Fraction(self.numerator * other.numerator,
                        self.denominator * other.denominator)

    def divide(self, other):
        if other.numerator == 0:
            raise ZeroDivisionError("cannot divide by 0")
        return Fraction(self.numerator * other.denominator,
                        self.denominator * other.numerator)

In main.py, import explicitly and keep names consistent: from fraction import Fraction; then use i = Fraction(7, 13), c = Fraction(3, 30), and call i.multiply(c) (note spelling). Either implement a set(self, n, d) method or replace r.set(20,45) with r = Fraction(20, 45). As suggested, the standard library fractions.Fraction is also a solid reference and, if you do not need this as an exercise, a drop-in solution.

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.