Python Forum

Full Version: What is wrong with this strings comparison
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I am struggling to find out what's wrong with my code.
I have two objects called ObjA and ObjB.
Both ObjA and ObjB have attributes named size, and the size is Very Large for both of them.


However when I do the comparison such as

if aObj.attributes['size'] == bObj.attributes['size']:
     print "They are equal"
It gave me an error saying :

Error:
if aObj.attributes['size'] == bObj.attributes['size']: KeyError: 'size'
I do not know why it gave me the error. Is it size a reserved key word so that I could not use it or is it some thing else that
I do not know ? Please help me if you could.

Many Thanks!
Please read BBCode to see how you should post your code on this forum.
Like this:
if aObj.size == bObj.size:
    print("They are equal")
can you show more of your code - class definitions for both objects and how you instantiate them, what is the value of attributes.
Minimal reproducible example is best. The error is clear - key error, no key 'size'
Sorry, I am re-posting my question in the correct format python code.

I have two python objects namely aObj, and bObj.
They both have the same attributes as following:


{'name': 'a', 'attributes': {'shape': 'diamond', 'size': 'very small', 'fill': 'yes'}}
{'name': 'b', 'attributes': {'shape': 'diamond', 'size': 'very small', 'fill': 'yes'}}
I tried to do a string comparison between aObj and bObj on the attribute size such as:

for aObjName in self.sortDict(a.objects):
                    aObj = a.objects[aObjName]
                    for bObjectName in self.sortDict (b.objects):
                        bObj = b.objects[bObjectName]
                        if aObj.attributes['shape'] == bObj.attributes['shape']:
                            transformDict ['Shape'] = aObj.attributes['shape'] + '-' + bObj.attributes['shape']
                            print ('Shape ' + transformDict ['Shape'])
                            
                        if aObj.attributes['fill'] == bObj.attributes['fill']:
                            transformDict['Fill'] = aObj.attributes['fill'] + '-' + bObj.attributes['fill']
                            print ('Fill ' + transformDict ['Fill'])
                            
                        if aObj.attributes['size'] == bObj.attributes['size']:
                            print ("two objects are the same");
I then got a Key error on ['size']. It worked fine on shape and fill attributes. Just did not work on attribute size for some unknown reason.

I do not know why it gave me the error. So I would be appreciated if you could help.

Thanks!

I thought I may have its working by having the following:
 if "size" in aObj.attributes and "size" in bObj.attributes:
                            if aObj.attributes['size'] == bObj.attributes['size']:
                                print ("size no change between aObj and bObj")
                            else:
                                print ("Size changed between aObj and bObj")