What to do with “Unexpected indent” in python?

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

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

17条回答
  •  独厮守ぢ
    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.

提交回复
热议问题