Python Forum
What is wrong with this strings comparison
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is wrong with this strings comparison
#1
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!
Reply
#2
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")
Reply
#3
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'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 757 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,533 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Splitting strings in list of strings jesse68 3 1,757 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  print function output wrong with strings. mposwal 5 3,107 Feb-12-2021, 09:04 AM
Last Post: DPaul
  python gives wrong string length and wrong character thienson30 2 2,989 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Finding multiple strings between the two same strings Slither 1 2,511 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lowercasing to strings before comparison SheeppOSU 4 2,717 Apr-27-2019, 02:33 AM
Last Post: rxndy
  lists, strings, and byte strings Skaperen 2 4,214 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020