python - 如何在 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”语句时笔记本仍然要求缩进
解决方案
问题是您没有告诉程序在满足第一个条件时要做什么(if 语句)。如果你不确定在 if 中该怎么做,你可以使用 python build in 'pass'。
if page_response.status_code == 200:
pass
else:
print(page_response.status_code)
推荐阅读
- java - 无法从远程 maven 存储库下载源,但从本地一切都很好
- angular - 反应式表单验证错误消息未在 CustomElement ANGULAR 的 UI 中更新
- chat - 如何在区块链中存储聊天消息?
- javascript - JS:在承诺链中时,“this”上下文一直显示为未定义
- c# - 格式化多行注释的任何扩展?
- ms-office - Microsoft Sway 不会在以前共享/显示的嵌入代码处自动刷新
- javascript - chrome dev-tool在调试带有时间戳的js文件时如何保留断点
- ansible - ansible chdir 模块在远程服务器上不起作用?
- .net - 在索引视图页面中显示下拉选择文本值
- gradle - Gradle exec 任务因“execCommand == null!”而失败