Home > Blockchain >  Can't figure out why there is an indentation error
Can't figure out why there is an indentation error

Time:06-13

I am getting an indentation error with this code and I can't figure out why

for i in [case_1, case_2]:
     if not(i[0] <= max(start_x, end_x) and i[0] >= min(start_x, end_x)):
         i = [10000000, 100000000]

CodePudding user response:

At the top level in modules, codes should start from the beginning of the line. (I can see from the raw-version of your question that you don't start for-loop from the beginning of the line)

Pep8 says 4 spaces for indentation but you don't have to. It's OK(but not recommended)for interpreter to have different number of spaces for indentation as long as the below lines are aligned and have same indentation

Here I used 10 spaces for for-loop but 1 space for if-statement:

for i in range(1):
          print("hi")
          print("hi")
          if 3 > 2:
           print("yes")

And it's perfectly fine. But you can't do something like:

for i in range(1):
          print("hi")
          print("hi")
          if 3 > 2:
           print("yes")
            print("no")  # <----- This has one space more(not aligned)

You can't also mix tabs with spaces. The common thing to do is to tell your editor to use spaces instead of tab characters. Also you can set it to automatically convert tabs to spaces "on-paste". This will help you not getting SyntaxError related to mixing tabs and spaces.

CodePudding user response:

No traceback in my end

for i in [case_1, case_2]:
    if not (i[0] <= max(start_x, end_x) and i[0] >= min(start_x,end_x)):
        i = [10000000, 100000000]

CodePudding user response:

By default, pythons indents are 4 spaces. The if statement seems to be indented by five. Try deleting a space.

  • Related