error in python: bad indentation [closed]

0

I'm new to python and I can not find the error since I put more spaces but I can not compile thank you very much

    
asked by sanlegas 28.12.2018 в 19:43
source

2 answers

3

In Python the indentation serves to mark where a new block starts and where it ends .

Although the way in which indents is free (you can use spaces or tabs and the number of them you want), it must be consistent .

By this it means that either you always use tabs, or always spaces. The second is recommended.

When a new block should start (for example, after line def funcion(): , or after a condition if condicion: , or after a loop start for , etc .. in general whenever a line ends with two points , the next line must be indented, that is, it must have more spaces on the left than the previous one.

This indicates for python that a new block starts (it would be the equivalent of opening a key in other programming languages).

All lines that make up that block must have the same indentation (the same number of spaces on the left). If another sub-block opens inside the block, it will be indented with more spaces.

Python gives the block terminated when a line suddenly has fewer spaces left than the previous line. This would be equivalent to "close key" in other languages. But in fact we may be finishing a nested block, which would require closing more than one key in other languages. How does python know how many (imaginary) keys it should close?

He knows this because the indentation of the line in question must match that of a previous line . That lets python know which "block" you return to after the nested block is finished.

That is precisely what your code fails you. You have this:

def main():
      linea1
     linea2
     linea3

After the header of the python function, wait for an indented line. Find it ( linea1 ). Everything is fine.

The following line should have the same indentation as linea1 (in which case it would still be part of the block, that is, the main() code), or, if it has less indentation, its indentation should match the of def main (in which case it is understood that this line is already outside the block and therefore is not part of main() .

In your case you find that the line linea2 has an indentation less than linea1 , but that does not match the indentation of def , therefore gives the error Unindent does not match any outer indentation , which in fact literally means "Disindentation does not fit with any other external indentation"

    
answered by 28.12.2018 / 20:11
source
0

Greetings first verify your code, you have a space on line 7, with respect to the following lines, 8 ... 9..etc.

I suggest you establish or define the tab to 4 spaces generally:

En el menú vas a View > Indentation > Tab width: 4.

According to what you want for me, I worked with 4.

For more information go to the documentation:

Sublimetext Documentation

I hope it helps you! !!

    
answered by 28.12.2018 в 19:58