Search code examples
pythonif-statementindentation

How to fix "IndentationError: expected an indented block" in Python?


I am trying to do a API test on a URL with Python, I have the following code block

       def simple_get(url):
        try:
            page_response = requests.get(page_link, timeout=5)
            if page_response.status_code == 200:
            # extract
            else:
                print(page_response.status_code)
                # notify, try again
        except requests.Timeout as e:
            print("It is time to timeout")
            print(str(e))
        except # other exception

When I run it give me the following error

File "<ipython-input-16-6291efcb97a0>", line 11
else:
   ^
IndentationError: expected an indented block

I dont understand why is the notebook still asking for indentation when I already have the "else" statement indented


Solution

  • Problem is that you did not tell the program what to do when the first condition is satisfied (if statement). If you are not sure about what to do in if, you can use python build in 'pass'.

    if page_response.status_code == 200:
        pass
    else:
        print(page_response.status_code)