user3458006
user3458006

Reputation: 1

Why am I getting an indentation error, "unexpected indent" in my code? Python

On the first line of my code, I seem to be getting an indentation error. I have 2 spaces as an indent on my code, as I always do since my class requires it, but for the first time I'm getting an error for it.

My code looks like this at the start.

  import math

  def main():

Upvotes: 0

Views: 2081

Answers (1)

Vasili Syrakis
Vasili Syrakis

Reputation: 9591

You should not indent Python code unless you have something like def, for, if and similar such things preceding the indentation block.

For example:

for thing in variable:
  #indent code
  pass

def aFunction():
  #indent
  pass

#invalid indentation:
 def aFunction2():
  pass

As you can imagine, the code not only becomes hard for human eyes to interpret, but also for your machine to interpret, because it cannot realise where the indentation starts or ends.

Speak to your teacher about this, they might be using an interpreter which allows them to indent their entire script by a particular amount. I would never recommend this though.

Upvotes: 2

Related Questions