[英]Python error "IndentationError: expected an indented block"
这是我的程序,我收到以下提到的错误:
def main():
print "hello"
if __name__=='__main__':
main()
File "hello.py", line 8
main()
^
IndentationError: expected an indented block
您的缩进不正确。 尝试这个 :
def main():
print "hello"
if __name__=='__main__':
main()
所有功能块以及if-else循环块都必须用制表符或4个空格(取决于环境)缩进。
if condition :
statements //Look at the indentation here
...
Out-of-block //And here
请参阅此以获得一些说明。
Normal Code
Indent block
Indent block
Indent block 2
Indent block 2
你应该做:
def main():
print "hello"
if __name__=='__main__':
main()
它可以是空格或制表符。
同样,缩进不需要在文件中相同,而仅对于每个块而言。
例如,您可以有一个这样的工作文件,但不建议这样做。
print "hello"
if True:
[TAB]print "a"
[TAB]i = 0
[TAB]if i == 0:
[TAB][SPACE][SPACE]print "b"
[TAB][SPACE][SPACE]j = i + 1
[TAB][SPACE][SPACE]if j == 1:
[TAB][SPACE][SPACE][TAB][TAB]print "c
您可能想要类似的东西:
def main():
print "hello"
if __name__=='__main__':
main()
注意缩进。 行开头的空白决定行的缩进级别,然后将其用于确定Python中语句的分组。
您的代码应如下所示:
def main():
print "hello"
if __name__=='__main__':
main()
只是分享这种愚蠢。 我有一个非常相似的错误IndentationError: expected an indented block def main()
因为我已经注释掉了以前的“some_function”的整个主体。
def some_function():
# some_code
# return
def main():
print "hello"
if __name__=='__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.