-2

I am looking for a way to print the two functions 1) introduce_itself (class Robot) and 2) give_command (class Command_from_Human) using the class use_robot.

However, my terminal in Visual Studio Code prints out the functions in a messy way and gives me a <main.use_robot object at 0x000001D037423F40>. Is there a solution to this error?

class Robot():
    def __init__(self, nam, weight, production_year):
        self.nam = nam
        self.weight = weight
        self.production_year = production_year
    
    def introduce_itself(self):
        print("The name of this robot is " + self.nam)
        print("The weight of my robot " + self.weight + " kg")
        print("This robot is produced in " + str(self.production_year))

class Commands_from_Human():
    def __init__(self, name, style, is_sitting):
        self.name = name
        self.style = style
        self.is_sitting = is_sitting
    
    def sit(self):
        self.is_sitting = True

    def standup(self):
        self.is_sitting = False
    
    def give_command(self):
        if self.is_sitting == True:
            print("My name is " + self.name + "." + " I am your owner.")
            print("My style of usage is " + self.style)
            print("Robot, sit!")

        if self.is_sitting == False:
            print("My name is " + self.name + "." + " I am your owner.")
            print("My style of usage is " + self.style)
            print("Robot, stand!")

class use_robot(Robot, Commands_from_Human):
    def __init__(self, nam, weight, production_year, name, style, is_sitting):
        Robot. __init__(self, nam, weight, production_year)
        Robot. introduce_itself(self)
        Commands_from_Human. __init__(self, name, style, is_sitting)
        Commands_from_Human. give_command(self)

trial_1 = use_robot("Simon", "100", 2000, "Paul", "Lazy", True)
print(trial_1)
daiman00
  • 25
  • 2

1 Answers1

0

That's not an error at all. When you print something in Python, it has to figure out how to print it. Most standard objects have a __str__ method that handles this, but with your own objects, it has no idea what to print, so it prints the name of the class and the object's id.

If you want your object to be printed in a particular way, then you need to provide a __str__ function that return the string. You might add:

    def __str__(self):
        return f"<Robot {self.nam},{self.weight},{self.production_year}"
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30