知识点
相关文章
更多最近更新
更多Python IndentationError:expected an indented block的解决方案
2019-03-16 23:54|来源: 领悟书生
看下面一段代码,意思是如果43大于42,就输出一段文本
>>> if 43>42: ... print("43大于42") File "<stdin>", line 2 print("43大于42") ^ IndentationError: expected an indented block |
不过出错了
在网站找到这样的一个解决方案:
Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的Python程序员,也可能陷入陷阱当中。最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分别的。 在编译时会出现这样的错IndentationError:expected an indented block说明此处需要缩进,你只要在出现错误的那一行,按空格或Tab(但不能混用)键缩进就行。 往往有的人会疑问:我根本就没缩进怎么还是错,不对,该缩进的地方就要缩进,不缩进反而会出错,,比如: if xxxxxx: (空格)xxxxx 或者 def xxxxxx: (空格)xxxxx 还有 for xxxxxx: (空格)xxxxx 一句话 有冒号的下一行往往要缩进,该缩进就缩进 |
再来看看正确的:
>>> if 43>42: ... print("43大于42") ... 43大于42 |
本文链接:Python IndentationError:expected an indented block的解决方案,转载请注明出版:http://www.656463.com/article/602
相关问答
更多-
这段代码完全没有问题,可能是你编辑器的问题,导致缩进对不上位置,另外我改写了下你的程序 ====================guess.py======== number = 23 while True: guess = raw_input('Enter an integer(\'q\' for quit) : ') #'q' for quit if guess == 'q': break guess = int(guess) if guess == number: print 'Congratulat ...
-
你的代码应该不全吧。凡是循环内部都要缩进1个空格。如果你if else缩进了,但是while也是循环,也需要缩进!所以while内部的if里需要缩进2个空格。你的错误就是循环没有加空格引起的。 #!/usr/bin/python import os ls=os.linesep fname = raw_input("please input file name:\n") #get filename while True: if os.path.exists(fname): print ("ERROR:'%s' ...
-
因为这里是给class定义方法,不是全局函数,所以应该空格。
-
缩进问题吧
-
这段代码完全没有问题,可能是你编辑器的问题,导致缩进对不上位置,另外我改写了下你的程序 ====================guess.py======== number = 23 while True: guess = raw_input('Enter an integer(\'q\' for quit) : ') #'q' for quit if guess == 'q': break guess = int(guess) if guess == number: print ' ...
-
如错误消息所示,您有一个缩进错误。 这可能是由标签和空格的混合造成的。 As the error message indicates, you have an indentation error. It is probably caused by a mix of tabs and spaces.
-
提示错误:IndentationError:预期缩进块(Prompting error:IndentationError: expected an indented block)[2022-06-28]
以下是您的代码在编辑器中的外观,该编辑器显示带有选项卡的所有空白字符(选项卡大小设置为4): 小点是空格字符,箭头是制表符。 如其他答案所述,混合标签和空格是一个坏主意。 上面的代码可能看起来不错,但看看如果将标签大小更改为8个字符会发生什么: 这是您看到的错误的根本原因。 当您混合制表符和空格时,您在编辑器中看到的缩进可能与Python解释器看到的缩进不同。 Here is what your code looks like in an editor that displays all of the wh ... -
可能是由于编辑或不正确的缩进。 你的代码完全在我的电脑上运行 ,但我会建议运行下面的代码 from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.debug = True app.run(port=8080) May be it is due the editor or ...
-
任务停止了,因为它没有更多的事情要做。 一个容器只有在做某事的时候才会运行。 docker ps -a命令可以用于检出容器。 The task stopped because it has nothing more to do. A container only runs as long as it's doing something. docker ps -a command can be useful to checkout the containers.
-
Crapy错误IndentationError:预期有一个缩进块(Crapy error IndentationError: expected an indented block)[2023-06-29]
你有两个这样的: def parse(self, response): def parse(self, response): You have two of these: def parse(self, response): def parse(self, response):