Why am I getting an indentation error?

Question

Why am I getting an indentation error?

Answer

If you’re seeing an error message that says something about an IndentationError it means you have code that isn’t indented properly somewhere. Remember, Python uses indentation (2 spaces per indentation on Codecademy) to determine blocks of code.
If you’ve written a return statement outside of a function (not indented inside to be inside the function), that will give you this error. For example:

def my_function():
  print "Hello!"
return 0  # This is a return outside of any function!
2 Likes

so according to codecademy on
https://www.codecademy.com/courses/learn-python/lessons/lists-and-functions/exercises/using-an-element-from-a-list-in-a-function

def list_function(x):
return x[1]

n = [3, 5, 7]
print list_function(n)

gives me the first result in the list and …

def list_function(x):
return x[0]

n = [3, 5, 7]
print list_function(n)

is the wrong answer even though we all know [0] accesses the first element in a list

The exercise isn’t asking for the first element of the list. It is asking for the element at index one i.e. the second element of the list. Remember indexing in python starts at 0.

Thanks for the reply. Codecademy can be confusing to me sometimes with the way things are worded.

1 Like