【发布时间】:2018-08-21 15:10:50
【问题描述】:
VSCode 在一些 python 代码上给了我缩进错误。我已经彻底检查了我的代码,看看它是否是混合制表符/空格问题(在代码中打开 render white space,然后将代码粘贴到 NPP 并打开 View tabs and spaces 和正则表达式搜索 \t等等) 但事实并非如此。任何地方都没有制表符。
所以我想知道是否所有函数都必须从它们的调用函数中缩进?
例如,这个语法有什么问题吗?
from SGS_ExcelPrePythonPrep_funcs_ver0_01JD import *
def main():
#some code
myVar = func1(data)
#more code
if __name__ == '__main__':
main()
#------ separate file: SGS_ExcelPrePythonPrep_funcs_ver0_01JD.py ------
def func1(data):
#more code
newVar = func2(moredata)
nutherVar = func3(data3)
#more code
def func2(moredata):
#WAS getting an indentation error here, until I intented
#more code
def func3(data3):
#now getting the indentation error here
#more code
func2() 是否需要在 func1() 下缩进?一直在谷歌搜索,但还没有找到这个特定问题的答案。显然我是一个糟糕的谷歌用户,因为这必须很容易找到信息。伸出援助之手?
更新:
这是我收到的错误消息:
File "w:\PyDev\SGS_ExcelPrePythonPrepAndFCode.py", line 13, in <module>
from SGS_ExcelPrePythonPrep_funcs_ver0_01JD import *
File "w:\PyDev\SGS_ExcelPrePythonPrep_funcs_ver0_01JD.py", line 198
def AlphaToIndex(ltr):
^
IndentationError: expected an indented block
更新 2:
modified original example pseudo-code to better represent two-file code structure
【问题讨论】:
-
你能不能尝试从命令行执行一下看看?
-
func2 不能为空;你会得到“IndentationError:期望一个缩进块。”添加
pass,如果您该功能什么都不做。 -
不,函数定义不需要在其调用函数下缩进。您能否提供更多详细信息,例如错误文本?
-
@JoshuaAvery - 这就是我的想法(以及问题的原因)。我更新了问题以添加错误消息。对我来说特别奇怪的是,当我双缩进接收错误的被调用函数时,错误会转移到下一个(被调用)函数,然后转移到下一个函数,等等。我对 python 很陌生,但我只是不记得以前必须缩进调用的函数。
-
你是在
main()函数里面定义函数吗?
标签: python-3.x