Hi guys,
I am working on a text based adventure game in Python and I am constantly getting this error:
There's an error in your program:
return outside function(g.py, line 179

Here is the code

http://www.mediafire.com/?4ga0weu4ifc6s1u

Recommended Answers

All 7 Replies

Reindent your code, I think indention is lost near line 126.

1.indentation is bad
2. return is outside a function

sorry Grib, that does not help. Any other ideas?

james lu, that's what the error tells me. I just don't get it

I seriously need help now!

I seriously need help now!

The rule is very simple: every time there is a colon : in python code, it is the beginning of a non empty indented block

def foo(): # <- see the colon here ?
    bar()  # <- this line is indented (preferably with 4 spaces)
    baz()  # <- this line too
qux() # <- not indented: this line is outside function foo.

or

if x == 0: # <- a colon
    foo()
    if y == 3.14: # <- a colon
        bar()
    else:  # <- else has the same indentation as the corresponding if
        baz()
    qux()
else: # <- a colon
    pass # <- a do-nothing statement to fill the else block
etc() # <- this line is not in a block

Read also this.

if it is indented, it means it's "With" anotther block of code.
you need to indent evry time there is a colon.If you've seen one already, double indent.

def foo(spam):
    print "bar" #part of the function "foo"
    print 5+4 #also part of "foo"
    if spam:
        print "eggs" #part of if and foo
        if 1+1 == 20:
            print"FAIL" #part of foo,if,if
        elif 1+1 == "2":
            print"FAIL" #part of foo if and elif
        print" done with if" #part of if and foo
    return 5 #part of foo and only foo
foo() #not part of foo because it isn't indented
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.