Python 如何校正函数
错误:Python 如何校正函数,python,Python,错误: test = pytesseract.image_to_string(img) def get_first_word(s): word = [] space = False for i in s: if i == ' ': space = True else: if space == False: word.append(i) get_
test = pytesseract.image_to_string(img)
def get_first_word(s):
word = []
space = False
for i in s:
if i == ' ':
space = True
else:
if space == False:
word.append(i)
get_first_word(test)
>>def获取第一个单词:
...
文件“”,第2行
^
缩进错误:应为缩进块
我已经三次检查了我的缩进,我确信它是正确的,但是vscode一直说它不是。有人能解释一下吗?考虑删除函数实现中的空行 当您将上述代码复制并粘贴到解释器时,在定义函数定义之后,解释器期望函数实现。函数定义后面的空行被解释为函数实现的结束,由于主体中不包含任何内容,解释器抛出缩进错误 要重新创建错误,请运行以下代码,例如:
def a():
()
在交互式解释器中缩进有点严格。一旦任何缩进块(函数、循环、尝试、如果…)启动,就应该避免空行,因为它们表示当前缩进级别的结束。与一次性翻译整个模块不同,交互式口译员无法知道接下来会发生什么,因此会尽可能急切地排练。谢谢您的回复!我已经尝试摆脱所有的空行,但它似乎没有解决问题。谢谢你的答复!但是我已经尝试过了,去掉所有的空行并不能修复错误。
>>> def get_first_word(s):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block