为什么会存在if执行了依然会执行elif的情况?

如题,是在一个案例的for循环中,if执行了,按理elif不应该执行,但是结果依然执行了elif,不太理解,是忽略了什么吗

下面的代码中,msg.tick > 0 执行了并打印了,elif中的else语句也执行打印了

for msg in track:

if isinstance(msg, midi.EndOfTrackEvent):
    print "end of track!!pass!!"
    continue  

if msg.tick > 0:
    print '##msg.tick > 0 !!!'
    current_tick += msg.tick
    print 'current_tick plus {} tick!'.format(msg.tick)
    print current_tick
    print msg.tick

if isinstance(msg, midi.NoteOnEvent) and msg.get_velocity() != 0:
    if len(notes[msg.get_pitch()]) > 0 and len(notes[msg.get_pitch()][-1]) != 2:
        if verbose:

            print("double NoteOn encountered,delete the first")
            print "the msg double note_on msg is {} \n".format(msg)
            print "the pitch is {}".format(msg.get_pitch())

    else:
        notes[msg.get_pitch()] += [[current_tick]]
        print notes[msg.get_pitch()]
        print [[current_tick]]
        print '#####this is noteonevent,,,the current_tick plus {} #####\n'.format(msg.get_pitch)

        # print 'the cu'

elif isinstance(msg, midi.NoteOffEvent) or (isinstance(msg, midi.NoteOnEvent) and msg.get_velocity() == 0):

    if len(notes[msg.get_pitch()][-1]) != 1:
        if verbose:
            print ("warning:skipping noteoff event with no corresponding noteon")
            print (msg)


    else:
        notes[msg.get_pitch()][-1] += [current_tick]
        print "the current_tick plus {} \n".format(notes[msg.get_pitch()][-1])

        print notes[msg.get_pitch()][-1]
        print [current_tick]
阅读 2.8k
2 个回答

因为 elif isinstance(msg, midi.NoteOffEvent) or (isinstance(msg, midi.NoteOnEvent) and msg.get_velocity() == 0): 这一句对应的 if 语句是 if isinstance(msg, midi.NoteOnEvent) and msg.get_velocity() != 0: 而不是 if msg.tick > 0:

你的分支是这样:
clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题