我有这个:
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
它告诉我“return”在函数之外。
当我把它改成这样时:
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
它只是告诉我有一个意外的缩进。我该如何解决这个问题?
最佳答案
在 Python 中,范围是通过与其他语言相同级别的缩进而不是大括号(即 {}
)定义的。
如果您编写一个函数,您需要确保所有函数体都处于同一级别的缩进 - 相同数量的空格或相同数量的制表符(混合空格和制表符会导致真正的困惑)。
在您的情况下,正确的缩进看起来类似于(我不完全知道,因为您没有发布整个功能的代码):
def function():
<indent>if colorkey is not None:
<indent><indent>if colorkey is -1:
<indent><indent><indent>colorkey = image.get_at((0,0))
<indent>image.set_colorkey(colorkey, RLEACCEL)
<indent>return image, image.get_rect()
关于python - 返回外部函数,或意外缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5626889/