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


Messages In This Thread
Relating 2 Classes - by wew044 - Feb-13-2020, 06:33 AM
RE: Relating 2 Classes - by buran - Feb-13-2020, 08:08 AM
RE: Relating 2 Classes - by wew044 - Feb-15-2020, 04:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  programming an interpolation polynom relating newton Hallo810 1 2,220 Nov-14-2020, 03:05 PM
Last Post: buran
  Using classes? Can I just use classes to structure code? muteboy 5 5,066 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