Bottom Page

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
Quote
#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') 
Quote
#3
I think I understood your explanation.
Thank you so much!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Using classes? Can I just use classes to structure code? muteboy 5 1,770 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)