What to do with “Unexpected indent” in python?

前端 未结 17 2365
天涯浪人
天涯浪人 2020-11-22 07:46

How do I rectify the error \"unexpected indent\" in python?

相关标签:
17条回答
  • 2020-11-22 08:09

    Run your code with the -tt option to find out if you are using tabs and spaces inconsistently

    0 讨论(0)
  • 2020-11-22 08:11

    Indentation in Python is important and this is just not for code readability, unlike many other programming languages. If there is any white space or tab in your code between consecutive commands, python will give this error as Python is sensitive to this. We are likely to get this error when we do copy and paste of code to any Python. Make sure to identify and remove these spaces using a text editor like Notepad++ or manually remove the whitespace from the line of code where you are getting an error.

    Step1 :Gives error 
    L = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
    print(L[2: ])
    
    Step2: L = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]print(L[2: ])
    
    Step3: No error after space was removed
    L = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
    print(L[2: ])
    OUTPUT: [[7, 8, 9, 10]]
    

    Thanks!

    0 讨论(0)
  • 2020-11-22 08:14

    By using correct indentation. Python is whitespace aware, so you need to follow its indentation guidlines for blocks or you'll get indentation errors.

    0 讨论(0)
  • 2020-11-22 08:17

    Python uses spacing at the start of the line to determine when code blocks start and end. Errors you can get are:

    Unexpected indent. This line of code has more spaces at the start than the one before, but the one before is not the start of a subblock (e.g. if/while/for statement). All lines of code in a block must start with exactly the same string of whitespace. For instance:

    >>> def a():
    ...   print "foo"
    ...     print "bar"
    IndentationError: unexpected indent
    

    This one is especially common when running python interactively: make sure you don't put any extra spaces before your commands. (Very annoying when copy-and-pasting example code!)

    >>>   print "hello"
    IndentationError: unexpected indent
    

    Unindent does not match any outer indentation level. This line of code has fewer spaces at the start than the one before, but equally it does not match any other block it could be part of. Python cannot decide where it goes. For instance, in the following, is the final print supposed to be part of the if clause, or not?

    >>> if user == "Joey":
    ...     print "Super secret powers enabled!"
    ...   print "Revealing super secrets"
    IndendationError: unindent does not match any outer indentation level
    

    Expected an indented block. This line of code has the same number of spaces at the start as the one before, but the last line was expected to start a block (e.g. if/while/for statement, function definition).

    >>> def foo():
    ... print "Bar"
    IndentationError: expected an indented block
    

    If you want a function that doesn't do anything, use the "no-op" command pass:

    >>> def foo():
    ...     pass
    

    Mixing tabs and spaces is allowed (at least on my version of Python), but Python assumes tabs are 8 characters long, which may not match your editor. Just say "no" to tabs. Most editors allow them to be automatically replaced by spaces.

    The best way to avoid these issues is to always use a consistent number of spaces when you indent a subblock, and ideally use a good IDE that solves the problem for you. This will also make your code more readable.

    0 讨论(0)
  • 2020-11-22 08:17

    Notepad++ was giving the tab space correct but the indentation problem was finally found in Sublime text editor.

    Use Sublime text editor and go line by line

    0 讨论(0)
  • 2020-11-22 08:21

    It depends in the context. Another scenario which wasn't yet covered is the following. Let's say you have one file with a class with a specific method in it

    class Scraper:
        def __init__(self):
            pass
    
        def scrape_html(self, html: str):
            pass
    

    and in the bottom of the file you have something like

    if __name__ == "__main__":
        # some
        # commands
        # doing
        # stuff
    

    making it the whole file look like this

    class Scraper:
        def __init__(self):
            pass
    
        def scrape_html(self, html: str):
            pass
    
    if __name__ == "__main__":
        # some
        # commands
        # doing
        # stuff
    

    If in scrape_html() you open up, for example, an if/else statement

    class Scraper:
        def __init__(self):
            pass
    
        def scrape_html(self, html: str):
            if condition:
                pass
            else:
    
    if __name__ == "__main__":
        parser = argparse.ArgumentParser()
    

    You'll need to add pass or whatever you want to to that else statement or else you'll get

    Expected indented block

    Unindent not expected

    Expected expression

    and in the first row

    Unexpected indentation

    Adding that pass would fix all of these four problems.

    0 讨论(0)
提交回复
热议问题