我正在运行下面提到的简单代码,但是得到indentation error
。我正在使用Python 3.x
和Juypter
笔记本。帮助将不胜感激。
import os
import sys
sys.path.insert(0, os.path.abspath('C:\dir python util'))
import h
h.my_first_function()
位于驱动器h.py
中的文件c:\dir python util
中的内容如下:
def my_first_function():
print ("my first program of python")
我得到的错误:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 5, in import h
File "C:\dir python util\h.py", line 2 print ("my first program of python") ^ IndentationError: expected an indented block
答案 0 :(得分:0)
File "C:\dir python util\h.py", line 2 print ("my first program of python") ^ IndentationError: expected an indented block
错误表明它很简单indentation error
。您需要在代码中保留适当的缩进。
缩进后的结果如下:
def my_first_function():
print ("my first program of python")
因此print
语句需要有4个空格而不是一个空格。
如果您还不了解Python的缩进规则,则建议您仔细阅读this文档。