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

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

for msg in track:
    print '-----------------------------------------------------'
    print "the msg is {} \n".format(msg)
    print '\n'

    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]
阅读 4.7k
3 个回答

1、你忽略了要贴代码
2、你要相信,一般bug不是语言造成的,而是写代码的人~~~

我猜测与前面多个同层的if有关系,如果只想前一个if为true不要执行elif,那就干脆改成else试一试。

或者改动一下结构, 实在没必要连续几个if同层

不存在单次进入if和elif分支的,只有一种可能就是你看错了。

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