我的代码中有缩进问题

时间:2019-07-28 15:08:45

标签: python python-3.x python-3.5

我正在做一些基于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编译错误:/ lyricsController / midievent1_callbacks
   文件“ / lyricsController / midievent1_callbacks”,第33行
     返回
          ^
  IndentationError:unindent与任何外部缩进级别都不匹配

     

文件“ / lyricsController / midievent1_callbacks”,第33行
     返回
          ^
  IndentationError:unindent与任何外部缩进级别都不匹配

2 个答案:

答案 0 :(得分:1)

Python是一种缩进范围的语言,用简单的英语来说,基本上意味着它使用缩进来确定哪行代码属于哪个范围级别。

例如,当python读取您的代码时,它会检查缩进级别以确定该行是否属于该行以及是否存在循环或函数或类似的东西。

您可以阅读有关here

的更多信息

答案 1 :(得分:0)

您应该阅读编译错误。它说第33行是错误所在。第33行是:

return

return语句也需要缩进。应该是:

    return

即,return语句的缩进量应与函数的缩进量相同。

也就是说,在这种情况下,我认为您甚至不需要return语句。您为什么不删除它,看看您的代码是否按预期工作?我想会的。

编辑:

下面的作者评论说,他们为return尝试了各种不同的缩进量,但没有一个起作用。这样做的原因是因为函数中的其他所有内容缩进的方式都太多了。每层应缩进一个缩进,标准通常是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个空格使另一行缩进,则会出现类似的错误!您需要为要缩进的方式选择一个规则,然后坚持执行。希望这会有所帮助。