Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SyntaxError :'return' outside function

Tags:

python

Compiler showed:

File "temp.py", line 56
    return result
SyntaxError: 'return' outside function

Where was I wrong?

class Complex (object):
    def __init__(self, realPart, imagPart):
        self.realPart = realPart
        self.imagPart = imagPart            

    def __str__(self):
        if type(self.realPart) == int and type(self.imagPart) == int:
            if self.imagPart >=0:
                return '%d+%di'%(self.realPart, self.imagPart)
            elif self.imagPart <0:
                return '%d%di'%(self.realPart, self.imagPart)   
    else:
        if self.imagPart >=0:
                return '%f+%fi'%(self.realPart, self.imagPart)
            elif self.imagPart <0:
                return '%f%fi'%(self.realPart, self.imagPart)

        def __div__(self, other):
            r1 = self.realPart
            i1 = self.imagPart
            r2 = other.realPart
            i2 = other.imagPart
            resultR = float(float(r1*r2+i1*i2)/float(r2*r2+i2*i2))
            resultI = float(float(r2*i1-r1*i2)/float(r2*r2+i2*i2))
            result = Complex(resultR, resultI)
            return result

c1 = Complex(2,3)
c2 = Complex(1,4)
print c1/c2

What about this?

class Complex (object):
    def __init__(self, realPart, imagPart):
        self.realPart = realPart
        self.imagPart = imagPart            

    def __str__(self):
        if type(self.realPart) == int and type(self.imagPart) == int:
            if self.imagPart >=0:
                return '%d+%di'%(self.realPart, self.imagPart)
            elif self.imagPart <0:
                return '%d%di'%(self.realPart, self.imagPart)
        else:
            if self.imagPart >=0:
                return '%f+%fi'%(self.realPart, self.imagPart)
            elif self.imagPart <0:
                return '%f%fi'%(self.realPart, self.imagPart)

    def __div__(self, other):
        r1 = self.realPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = float(float(r1*r2+i1*i2)/float(r2*r2+i2*i2))
        resultI = float(float(r2*i1-r1*i2)/float(r2*r2+i2*i2))
        result = Complex(resultR, resultI)
        return result

c1 = Complex(2,3)
c2 = Complex(1,4)
print c1/c2
like image 951
lavitanien Avatar asked Oct 25 '12 11:10

lavitanien


People also ask

How to solve ‘return outside function’ syntax error in Python?

You can refer to the below screenshot python syntaxerror: ‘return’ outside function To solve this SyntaxError: return outside function python we need to check the code whether the indentation is correct or not and also the return statement should be inside the function so that this error can be resolved.

Why do I get a SyntaxError when returning from a function?

Often, when one is confronted with a return outside function Python SyntaxError, it's due to improper indentation. Indentation tells Python that a series of statements and operations belong to the same block or scope. When you place a return statement at the wrong level of indentation, Python won't know which function to associate it with.

What does return external function mean in Python?

A return statement sends a value of a function to a main program. If you specify a return declaration outside of a function, you will encounter the message "SyntaxError: ’return’ external function" . Error In this guide, we explore what the external "return" function " means and why it is triggered.

What does it mean when it says return outside function?

In some cases, the syntax error will say ’return’ outside function. To put it simply, this means that you tried to declare a return statement outside the scope of a function block. The return statement is inextricably linked to function definition.


1 Answers

I would check my indentation, it looks off. Are you possibly mixing tabs and spaces? The PEP8 (Python Style Guide) recommends using 4 spaces only. Unlike other languages, whitepace makes a big difference in Python, so consistency is important.

The above also makes the following recommendation:

When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!

In particular, your 2nd else seems to be off (probably should be indented), and this method def __div__(self, other): too (which I would think ought to be at the same level as your other defs - i.e., moved "out" rather than indented).

Problems mixing tabs/blanks are easy to have since both characters are "invisible".

like image 53
Levon Avatar answered Oct 13 '22 20:10

Levon