首页 > 解决方案 > 如何在 Python 中修复“IndentationError:预期有缩进块”?

问题描述

我正在尝试使用 Python 对 URL 进行 API 测试,我有以下代码块

       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

当我运行它给我以下错误

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

我不明白为什么当我已经缩进了“else”语句时笔记本仍然要求缩进

标签: pythonif-statementindentation

解决方案


问题是您没有告诉程序在满足第一个条件时要做什么(if 语句)。如果你不确定在 if 中该怎么做,你可以使用 python build in 'pass'。

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

推荐阅读