Home > Software design >  Python - Stopping on first IF condition
Python - Stopping on first IF condition

Time:11-19

I am struggling to understand why my script only matches the first key in the dictionary and doesn't proceed to process other keys.

My script generates the command I need if say only tcp ports are present but if I try to mix this and have other ports(keys) it will not process the other elif. Whether I put this in a for loop or not I seem to be getting the same results. Im new to this so I might have missed something basic here.

ports = {'tcp': [['10', '20']], 'rant': [['100', '200']], 'cm': ['https']}

if __name__ == "__main__":
    obj = []
    tcpport = []
    udpport = []
    mxport = []
    if ports.get("tcp"):
        tcp2 = ports.get("tcp")
        tcpport = True
        for t in compile_port2(tcp2):
            obj.append(t)
            print("compiled tcp only objects")
    elif ports.get("udp"): 
        udpport = True
        udp2 = ports.get("udp")
        for u in compile_port2(udp2):
            obj.append(u)
            print("compiled udp only objects")
    elif ports.get("rant"):
        rant2 = ports.get("rant")
        tcpport = True
        ran_tcp2 = (list(it.zip_longest(*[iter(rant2[0])] * 2)))
        for rt in compile_rant_port2(ran_tcp2):
            obj.append(rt)
            print("compiled tcp only range objects")
    elif ports.get("ranu"):
        ranu2 = ports.get("ranu")
        udpport = True
        ran_udp2 = (list(it.zip_longest(*[iter(ranu2[0])] * 2))) # split strings by groups of 2 for port ranges  
        for ru in compile_rant_port2(ran_udp2):
            obj.append(ru)
            print("compiled udp only range objects")
    elif ports.get("cm"):
        tcpport = True
        cm2 = ports.get("cm")
        for c in compile_port2(cm2):
            obj.append(c)
            print("compiled worded objects\n\n")
    else:
        print("no items found")

CodePudding user response:

The use of elif is to evaluate an else-if condition. So if the if condition is matched, it will not match any other elifs'.

If you want to process all the keys, you should multiple if conditions and not elif.

See below, a modified version

ports = {'tcp': [['10', '20']], 'rant': [['100', '200']], 'cm': ['https']}

if __name__ == "__main__":
    obj = []
    tcpport = []
    udpport = []
    mxport = []
    items_found = False
    if "tcp" in ports:
        items_found=True
        tcp2 = ports.get("tcp")
        tcpport = True
        for t in compile_port2(tcp2):
            obj.append(t)
            print("compiled tcp only objects")
    if "udp" in ports:
        items_found = True
        udpport = True
        udp2 = ports.get("udp")
        for u in compile_port2(udp2):
            obj.append(u)
            print("compiled udp only objects")
    if "rant" in ports:
        items_found = True
        rant2 = ports.get("rant")
        tcpport = True
        ran_tcp2 = (list(it.zip_longest(*[iter(rant2[0])] * 2)))
        for rt in compile_rant_port2(ran_tcp2):
            obj.append(rt)
            print("compiled tcp only range objects")
    if "ranu" in ports:
        items_found = True
        ranu2 = ports.get("ranu")
        udpport = True
        ran_udp2 = (list(it.zip_longest(*[iter(ranu2[0])] * 2)))  # split strings by groups of 2 for port ranges  
        for ru in compile_rant_port2(ran_udp2):
            obj.append(ru)
            print("compiled udp only range objects")
    if "cm" in ports:
        items_found = True
        tcpport = True
        cm2 = ports.get("cm")
        for c in compile_port2(cm2):
            obj.append(c)
            print("compiled worded objects\n\n")
            
    if not items_found:
        print("no items found")

CodePudding user response:

Am I right in understanding that you are asking why the first clause

if ports.get("tcp"):
    tcp2 = ports.get("tcp")
    tcpport = True
    for t in compile_port2(tcp2):
        obj.append(t)
        print("compiled tcp only objects")

Is the only one which runs when "tcp" is present in ports? That's how if elif else blocks are supposed to work. Elif stands for else if, i.e, "If the thing that came before we WASNT TRUE, then try this next"

If you want to to multiple things, then use many if blocks rather than elif.

  • Related