Python Forum
Relating 2 Classes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Relating 2 Classes (/thread-24419.html)



Relating 2 Classes - wew044 - Feb-13-2020

Hello everyone,

I have a robot class and a person class, I am trying to do the following:

1. find the person who is sitting
2. then use input function to type in robot's name
3. assign the robot to the person who is sitting

I am having trouble with the first step which when I loop through the person dictionary, it is not able to find the person who is sitting. If anyone could provide some guidance that would be great, or maybe redirect my approach if it is not right. Thanks!

#robot class, with object name, color, weight, and selfintroduce function
class Robot():
    def __init__ (self, name, color, weight): #defining attributes name, color, weight
        self.name = name
        self.color = color
        self.weight = weight
    def selfintroduce(self): #defining selfintroduce function
        print("The robot name is", self.name)
        print("The color is",self.color)
        print("The weight is",self.weight)

robots = {"zero":Robot("Zero","white","100lb"),"r2d2":Robot("R2D2", "blue", "200lb")}

class Person():
    def __init__ (self, name, personality, sitting):
        self.name = name
        self.personality = personality
        self.sitting = sitting
    def sit_down(self):
        self.sitting = True
    def stand_up(self):
        self.sitting = False

p = {"alice":Person("Alice","Aggressive",False), "becky":Person("Becky","Talkative",True)}
print('The list of people are: '+', '.join(p.keys()))

#print(bool(Person.sit_down(p["becky"]))) #how come this doesnt return the right boolean?

#find the person who is sitting, then input robot, assign the robot to the person.

#assign_robot = input('assign a robot to the person who is sitting\n')

for key, value in p.items():
    if p[key].sit_down() is False: 
        print(key)  # wont print anything, why



RE: Relating 2 Classes - buran - Feb-13-2020

Person.sit_down() is method that does not return anything, so it returns None
None is False will never be True, so line 35 never got executed.

Now, you don't want to use the method, you want to use Person.sitting property
Then you don't have to use p[key], that is in fact value
And you should not use is False

class Robot():
    def __init__ (self, name, color, weight): #defining attributes name, color, weight
        self.name = name
        self.color = color
        self.weight = weight
    def selfintroduce(self): #defining selfintroduce function
        print("The robot name is", self.name)
        print("The color is",self.color)
        print("The weight is",self.weight)
 
robots = {"zero":Robot("Zero","white","100lb"),"r2d2":Robot("R2D2", "blue", "200lb")}
 
class Person():
    def __init__ (self, name, personality, sitting):
        self.name = name
        self.personality = personality
        self.sitting = sitting
    def sit_down(self):
        self.sitting = True
    def stand_up(self):
        self.sitting = False
 
persons = {"alice":Person("Alice","Aggressive",False), "becky":Person("Becky","Talkative",True)}

for key, person in persons.items():
    if person.sitting: 
        print(f'{person.name} is currently sitting') 



RE: Relating 2 Classes - wew044 - Feb-15-2020

I think I understood your explanation.
Thank you so much!