我的代码中有缩进问题
I have a problem with indentation in my code
我正在做一些基于 TouchDesigner 的脚本 python3.5,其中一个有缩进错误。拜托,我需要帮助我几天前才开始使用 python 的人。提前致谢。
# me - this DAT
#
# dat - the DAT that received the event
# rowIndex - the row number that was added
# message - a readable description of the event
# channel - the numeric value of the event channel
# index - the numeric value of the event index
# value - the numeric value of the event value
# input - true when the event was received
# bytes - a byte array of the received event
#
# Example:
# message channel index value bytes
# Note On 1 63 127 90 2f 127
def onReceiveMIDI(dat, rowIndex, message, channel, index, value, input, bytes):
x = op('/lyricsController/Lyrics_Texts/convert1').numRows - 1
if x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 17:
op('/lyricsController/passadorLyrics_const').par.value0 += 1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'PASSANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 18:
op('/lyricsController/passadorLyrics_const').par.value0 += -1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'VOLTANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 19:
op('/lyricsController/passadorLyrics_const').par.value0 = 0
op('/lyricsController/passadorLyrics_const').par.name0 = 'RESETADO'
return
这是口译员反馈:
DAT compile error: /lyricsController/midievent1_callbacks
File "/lyricsController/midievent1_callbacks", line 33
return
^
IndentationError: unindent does not match any outer indentation level
File "/lyricsController/midievent1_callbacks", line 33
return
^
IndentationError: unindent does not match any outer indentation level
你应该阅读编译错误。它说第 33 行是错误所在。第 33 行是:
return
return 语句也需要缩进。应该是:
return
即,return 语句应与函数一样缩进。
也就是说,在这种情况下,我认为您甚至不需要 return 语句。为什么不直接删除它,然后查看您的代码是否按预期工作?我觉得会的。
编辑:
作者在下面评论说,他们为 return
尝试了多种不同的缩进量,并且 none 奏效了。这样做的原因是因为函数中的其他所有内容都缩进了太多。每层要缩进一个缩进,标准通常是1个缩进=1个制表符或4个空格。所以这个:
def onReceiveMIDI(dat, rowIndex, message, channel, index, value, input, bytes):
x = op('/lyricsController/Lyrics_Texts/convert1').numRows - 1
if x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 17:
op('/lyricsController/passadorLyrics_const').par.value0 += 1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'PASSANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 18:
op('/lyricsController/passadorLyrics_const').par.value0 += -1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'VOLTANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 19:
op('/lyricsController/passadorLyrics_const').par.value0 = 0
op('/lyricsController/passadorLyrics_const').par.name0 = 'RESETADO'
return
...应该是这样的:
def onReceiveMIDI(dat, rowIndex, message, channel, index, value, input, bytes):
x = op('/lyricsController/Lyrics_Texts/convert1').numRows - 1
if x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 17:
op('/lyricsController/passadorLyrics_const').par.value0 += 1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'PASSANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 18:
op('/lyricsController/passadorLyrics_const').par.value0 += -1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'VOLTANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 19:
op('/lyricsController/passadorLyrics_const').par.value0 = 0
op('/lyricsController/passadorLyrics_const').par.name0 = 'RESETADO'
return
注意:
1、每一层代码都有一定的缩进量。第一层,例如 x = op(...
,缩进 4 个空格。然后 if
语句中的内容缩进 8 = 4 * 2 个空格。等等
2. return
语句应该不会出现在 if
语句中,而是出现在代码主体中,因此它的缩进量与代码主体相同。
我说你根本不需要 return
语句是正确的,但值得一开始就解释为什么会出现此错误,即使你尝试缩进语句时也是如此,因为你会将来可能 运行 加入这个。事实上,如果您使用制表符缩进一行并使用 4 个空格缩进另一行,就会出现这样的错误!您需要为缩进方式选择一个规则,然后坚持使用它。希望这有帮助。
Python是缩进作用域语言,通俗的说就是用缩进来判断哪一行代码属于哪个作用域级别。
例如,当 python 读取您的代码时,它会检查缩进级别以确定该行是否属于以及是否属于循环或函数或类似的东西。
您可以阅读更多关于 here
我正在做一些基于 TouchDesigner 的脚本 python3.5,其中一个有缩进错误。拜托,我需要帮助我几天前才开始使用 python 的人。提前致谢。
# me - this DAT
#
# dat - the DAT that received the event
# rowIndex - the row number that was added
# message - a readable description of the event
# channel - the numeric value of the event channel
# index - the numeric value of the event index
# value - the numeric value of the event value
# input - true when the event was received
# bytes - a byte array of the received event
#
# Example:
# message channel index value bytes
# Note On 1 63 127 90 2f 127
def onReceiveMIDI(dat, rowIndex, message, channel, index, value, input, bytes):
x = op('/lyricsController/Lyrics_Texts/convert1').numRows - 1
if x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 17:
op('/lyricsController/passadorLyrics_const').par.value0 += 1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'PASSANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 18:
op('/lyricsController/passadorLyrics_const').par.value0 += -1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'VOLTANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 19:
op('/lyricsController/passadorLyrics_const').par.value0 = 0
op('/lyricsController/passadorLyrics_const').par.name0 = 'RESETADO'
return
这是口译员反馈:
DAT compile error: /lyricsController/midievent1_callbacks
File "/lyricsController/midievent1_callbacks", line 33
return
^
IndentationError: unindent does not match any outer indentation levelFile "/lyricsController/midievent1_callbacks", line 33
return
^
IndentationError: unindent does not match any outer indentation level
你应该阅读编译错误。它说第 33 行是错误所在。第 33 行是:
return
return 语句也需要缩进。应该是:
return
即,return 语句应与函数一样缩进。
也就是说,在这种情况下,我认为您甚至不需要 return 语句。为什么不直接删除它,然后查看您的代码是否按预期工作?我觉得会的。
编辑:
作者在下面评论说,他们为 return
尝试了多种不同的缩进量,并且 none 奏效了。这样做的原因是因为函数中的其他所有内容都缩进了太多。每层要缩进一个缩进,标准通常是1个缩进=1个制表符或4个空格。所以这个:
def onReceiveMIDI(dat, rowIndex, message, channel, index, value, input, bytes):
x = op('/lyricsController/Lyrics_Texts/convert1').numRows - 1
if x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 17:
op('/lyricsController/passadorLyrics_const').par.value0 += 1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'PASSANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 18:
op('/lyricsController/passadorLyrics_const').par.value0 += -1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'VOLTANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 19:
op('/lyricsController/passadorLyrics_const').par.value0 = 0
op('/lyricsController/passadorLyrics_const').par.name0 = 'RESETADO'
return
...应该是这样的:
def onReceiveMIDI(dat, rowIndex, message, channel, index, value, input, bytes):
x = op('/lyricsController/Lyrics_Texts/convert1').numRows - 1
if x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 17:
op('/lyricsController/passadorLyrics_const').par.value0 += 1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'PASSANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 18:
op('/lyricsController/passadorLyrics_const').par.value0 += -1.0
op('/lyricsController/passadorLyrics_const').par.name0 = 'VOLTANDO'
elif x > 0 and op('/lyricsController/midievent1')[1,0] == 'Note On' and op('/lyricsController/midievent1')[1,2] == 19:
op('/lyricsController/passadorLyrics_const').par.value0 = 0
op('/lyricsController/passadorLyrics_const').par.name0 = 'RESETADO'
return
注意:
1、每一层代码都有一定的缩进量。第一层,例如 x = op(...
,缩进 4 个空格。然后 if
语句中的内容缩进 8 = 4 * 2 个空格。等等
2. return
语句应该不会出现在 if
语句中,而是出现在代码主体中,因此它的缩进量与代码主体相同。
我说你根本不需要 return
语句是正确的,但值得一开始就解释为什么会出现此错误,即使你尝试缩进语句时也是如此,因为你会将来可能 运行 加入这个。事实上,如果您使用制表符缩进一行并使用 4 个空格缩进另一行,就会出现这样的错误!您需要为缩进方式选择一个规则,然后坚持使用它。希望这有帮助。
Python是缩进作用域语言,通俗的说就是用缩进来判断哪一行代码属于哪个作用域级别。
例如,当 python 读取您的代码时,它会检查缩进级别以确定该行是否属于以及是否属于循环或函数或类似的东西。
您可以阅读更多关于 here