Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Relating 2 Classes
#1
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
Reply
#2
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') 
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
#3
I think I understood your explanation.
Thank you so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  programming an interpolation polynom relating newton Hallo810 1 2,166 Nov-14-2020, 03:05 PM
Last Post: buran
  Using classes? Can I just use classes to structure code? muteboy 5 4,978 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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