Hi all,

Here is a part of my code:

#-- if the ports are mapped properly
            if(map_dict1[int(MIO_outports1[i])] == int(Exp_map_inport[i]) ):
                flag = 1
            elif(map_dict2[int(MIO_outports1[i])] == int(Exp_map_inport[i])):
                flag = 1
            else:
                flag = 0

But I am not able to navigate to the 'elif' part. I am getting :

if(map_dict1[int(MIO_outports1[i])] == int(Exp_map_inport[i]) ):
KeyError: 596

but my key '596' is present in 'map_dict2'. Why am I not able to go to the elif part?? where am i going wrong??

Recommended Answers

All 4 Replies

> but my key '596' is present in 'map_dict2'.

but according to the error, it's not present in map_dict1. Check for that using map_dict1.has_key(key) (or the new and preferred "key in map_dict1") before checking whether the value at that key equals the expected value.

commented: U showed me the right direction :) +3

thanks raptr_dflo, but what I say is the key is present in 'map_dict2' so, why is the control not getting passed to the 'elif' part,?? when key is not present in 'map_dict1' it should check 'map_dict2' right??

ye i got what u were saying raptr_dflo :)
i changed it to:

#-- if the ports are mapped properly
            #-- if the dictionary has key 
            if(map_dict1.has_key(int(MIO_outports1[i]))):
                #-- if MIO outport is properly mapped to Exp inport(as defined in dictionary)
                if(map_dict1[int(MIO_outports1[i])] == int(Exp_map_inport[i]) ):
                    flag = 1
            elif(map_dict2.has_key(int(MIO_outports1[i]))):
                if(map_dict2[int(MIO_outports1[i])] == int(Exp_map_inport[i])):
                    flag = 1
            else:
                flag = 0

n it's working .. thank you :)

You should add as first condition

MIO_outports1[i] in map_dict1

to only access the other part if i is not in the dictionary.

Actually thinking about what you want to accomplish, you just want to do:

test = any(d[int(MIO_outports1[i])] == int(Exp_map_inport[i]) for d in (map_dict1, map_dict2) if MIO_outports1[i] in d)

Propably your code around is also trying to do something wrong way and this all is not necessary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.