[Tutor] Unexpected Indent Error Message

Alan Gauld alan.gauld at yahoo.co.uk
Fri Mar 27 13:33:19 EDT 2020


On 27/03/2020 17:18, Alan Thwaits wrote:
> I've aligned 'death' with the print above it (checking with VSCode to be
> certain of the alignment), and get the following:

>   File "C:/Users/Alan/Documents/Python/ex43.py", line 158
>     return 'death'
>     ^
> SyntaxError: 'return' outside function

Its not the death that needs to be aligned its the return.
You should review the rulkes of indentation in Python, they really are
critical.

Essentially the indentation level determines what is inside the
function. In this case the first line (print) sets the required indent.
All other lines in the function must align with that. (or deeper if they
are sub blocks)

>>> class EscapePod(Scene):
>>>
>>>     def enter(self):
>>>         print(dedent("""
>>>             You rush through the ship, desperately trying to make it to
>>>         return 'death'
...
>>>         if....
>>>         else:
>>>             print(dedent("""...

As above. Notice how all the top level lines are at the same
indent under the function def line? That is crucial.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list