Why am I getting "IndentationError: expected an indented block"
Why am I getting "IndentationError: expected an indented block"
我是 Python 的新手,所以请怜悯我。
我的环境是:
macOS Sierra 10.12.4 and Anaconda 4.3.16
我必须遵循以下代码:
import os,
import urllib.request
IRIS_TRAINING = "iris_training.csv"
IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"
if not os.path.exists(IRIS_TRAINING):
raw = urllib.request.urlopen(IRIS_TRAINING_URL).read()
我在 Anaconda Spyder 中 运行,当我使用 F9 单步执行代码时,我得到:
>>> import os
>>> import urllib.request
>>> IRIS_TRAINING = "iris_training.csv"
>>> IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"
>>> if not os.path.exists(IRIS_TRAINING):
... raw = urllib.request.urlopen(IRIS_TRAINING_URL).read()
File "<stdin>", line 2
raw = urllib.request.urlopen(IRIS_TRAINING_URL).read()
^
IndentationError: expected an indented block
为什么会出现此错误?
查尔斯
(Spyder developer here) Spyder 中的 运行 一行 功能(当您按 F9) 用于 运行 完整命令,即立即产生结果的行。在你的例子中,行
if not os.path.exists(IRIS_TRAINING):
不是产生结果的东西,因此它会在下一行产生错误。
因此,我建议您不要使用 F9,而是使用单元格。
注意:单元格是代码块,您可以通过编写 #%%
形式的代码注释来创建。它们是 运行,编辑器 Shift+Enter。
我有
'''
^
IndentationError: expected an indented block
并且找不到导致它的原因。看来我不小心在代码的开头做了一个缩进:)
我删除了这个缩进,一切都变好了!
我是 Python 的新手,所以请怜悯我。 我的环境是:
macOS Sierra 10.12.4 and Anaconda 4.3.16
我必须遵循以下代码:
import os,
import urllib.request
IRIS_TRAINING = "iris_training.csv"
IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"
if not os.path.exists(IRIS_TRAINING):
raw = urllib.request.urlopen(IRIS_TRAINING_URL).read()
我在 Anaconda Spyder 中 运行,当我使用 F9 单步执行代码时,我得到:
>>> import os
>>> import urllib.request
>>> IRIS_TRAINING = "iris_training.csv"
>>> IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"
>>> if not os.path.exists(IRIS_TRAINING):
... raw = urllib.request.urlopen(IRIS_TRAINING_URL).read()
File "<stdin>", line 2
raw = urllib.request.urlopen(IRIS_TRAINING_URL).read()
^
IndentationError: expected an indented block
为什么会出现此错误?
查尔斯
(Spyder developer here) Spyder 中的 运行 一行 功能(当您按 F9) 用于 运行 完整命令,即立即产生结果的行。在你的例子中,行
if not os.path.exists(IRIS_TRAINING):
不是产生结果的东西,因此它会在下一行产生错误。
因此,我建议您不要使用 F9,而是使用单元格。
注意:单元格是代码块,您可以通过编写 #%%
形式的代码注释来创建。它们是 运行,编辑器 Shift+Enter。
我有
'''
^
IndentationError: expected an indented block
并且找不到导致它的原因。看来我不小心在代码的开头做了一个缩进:)
我删除了这个缩进,一切都变好了!