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"}


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

x = AIEntity("Test")

The output is only

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
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.
Due to the reasons discussed here we will be moving to on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 1672
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.
Posts: 3
Joined: Tue Apr 22, 2014 9:41 pm

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests