Checking Data In dict

This is the place for queries that don't fit in any of the other categories.

Checking Data In dict

Postby Auslander » Tue Apr 22, 2014 9:48 pm

My goal is to have the program read through my library, and check if any value is not 100. If it is not 100, I want it to simply print the value and respective element name.

I attempted to do this using a for loop, but it doesn't seem to be working out well. I can't find my error.

This is what I have.
Code: Select all
class AIEntity(object):
    def __init__(self,ID):
        self.ID = ID #Name for entity
        self.LArm = 10 #Left Arm Health
        self.RArm = 100 #Right Arm Health
        self.LLeg = 100 #Left Leg Health
        self.RLeg = 10 #Right Leg Health
        self.Torso = 10 #Torso Health
        self.Head = 100 #Head Health
        self.Body = {self.LArm:"Left Arm", self.RArm:"Right Arm", self.LLeg:"Left Leg", self.RLeg:"Right Leg", self.Torso:"Torso", self.Head:"Head"}

        self.checkBody()

    def checkBody(self):
        for value, part in self.Body.iteritems():
            if value != 100:
                print value
                print part


x = AIEntity("Test")


The output is only
10
Torso

So it is only printing the x.Torso value. Idk why. Any help? Thanks. :)
Last edited by Yoriz on Tue Apr 22, 2014 10:03 pm, edited 1 time in total.
Reason: First post lock
Auslander
 
Posts: 3
Joined: Tue Apr 22, 2014 9:41 pm

Re: Checking Data In Library

Postby Yoriz » Tue Apr 22, 2014 10:01 pm

Dictionary keys are unique, you are setting the key as 10 or 100 so your dictionary will only have 2 entrys 10 & 100 and there values will be the last ones set to those keys.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 833
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Checking Data In dict

Postby Auslander » Wed Apr 23, 2014 12:08 am

Oh god. I'm an idiot. Lol. Thank you.
Auslander
 
Posts: 3
Joined: Tue Apr 22, 2014 9:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests