Unexpected Indent

Hello. I am curious as to why adding an indent causes an error. Here is my code and my explanation below with stars.

Your code below:

def generate_trip_instructions(location):

print("Looks like you are planning a trip to visit " + location)

print(“You can use the public subway system to get to” + location)

generate_trip_instructions(" Central Park")

generate_trip_instructions(" Grand Central Station")
File “travel.py”, line 6
** generate_trip_instructions(" Grand Central Station")**
** ^**
IndentationError: unexpected indent

Did you skip the explanation of Python block syntax?

I’m not sure where that is, can you re-direct me please?

One is guessing it will be near the front end of the Learn Python course, both 2 and 3. Indentation is fundamental to how Python identifies code blocks nested in functions, loops, for statements, etc. Indentation of two lines must match if they are contained in same block. Tabs and spaces are not interchangeable and this leads to a lot of early learning snags when the code looks okay, but the interpreter hits a snag with disagreeing indentation.

Bottom line, use spaces, not tabs. Auto indentation varies in IDEs so if in doubt, replace the auto inserted indentation with your own space indents.

if condition:
    # action if truthy
    # code line 2
    # code line 3
else:
    # default action
    # code line 2
    # code line 3

Whenever a line ends with a colon, it is the signature line of the indented code body following it. All such usage has the indent in common.

Just as code can be nested in any language, often with blocks denoted by curly braces, it can be nested in Python, along with the proviso that each nested block be indented accordingly.

Read up and practice with this syntax concept until you are comfortable. It should come to us as first nature, going forward.

Thank you for your reply. I appreciate the help!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.