1

I'm new to Python and OOP. I have previously done python in procedural programming style.

I need to write a class named Pet, which should have the following data attributes:

  1. __name (for the name of a pet)
  2. __animal_type (for the type of a pet, such as Dog, Cat)
  3. __age (for the pet’s age)

It should contain an __init__initializer that creates these attributes. It should also have all the mutator and accessor methods. Write a program that creates an object of the class and prompts the user to enter the name, type, and age of his pet. At the end of the program, display the pet’s name, type, and age on the screen. The sample output is given below.

Enter Pet Name: Lucky
Enter Pet Type: Dog
Enter Pet Age: 5

Pet Lucky is a dog, and it is 5 years old

This is what I have done so far (it doesn't run on my cmd, there is no error code)

class Pet:
    def set_name(self, name):
        self.__name = name

    def set_animal_type(self, animal_type):
        self.__animal_type = animal_type

    def set_age(self,age):
        self.__age = age

    def get_name(self):
        return self.__name

    def get_animal_type(self):
        return self.__animal_type

    def get_age(self):
        return self.__age

name = input("Enter pet name: ")
animal_type = input("Enter pet type: ")
age = input("Enter pet age: ")


p = Pet()
p.set_name(name)
p.set_animal_type(animal_type)
p.set_age(age)

print("Pet %s is a %s ,and it is %s years old." %(p.set_name(), p.set_animal_type(), p.set_age()))
3
  • You should probably do some more research Commented Oct 16, 2019 at 17:47
  • I hesitate to ask, but why do you need getters and setters? That's the kind of assignment someone teaching Java in Python would give. Commented Oct 16, 2019 at 18:04
  • In Python, you wouldn't bother with mangled attribute names, getters that do nothing but return the value of an attribute, and setters that do nothing but set the value of an attribute. Commented Oct 16, 2019 at 18:10

2 Answers 2

2

You're calling setter instead of getter Change your print line as follows

print("Pet %s is a %s ,and it is %s years old." %(p.get_name(), p.get_animal_type(), p.get_age()))

Also, add missing init method

    def __init__(self, name, animal_type, age):
        self.name = name
        self.animal_type = animal_type
        self.age = age
Sign up to request clarification or add additional context in comments.

2 Comments

You might want to address the missing __init__ method too.
@potatoooooooooo you can accept the answer if this solves your problem
1

You should read about class method and espeacilly dunder method, here is what you are asking to do.

class Pet:

    def __init__(self, name, animal_type, age):
        self.name = name
        self.animal_type = animal_type
        self.age = age

    def __str__(self):
        return f"Pet {self.name} is a {self.animal_type} ,and it is {self.age} years old." 

    def __repr__(self):
        return self.__str__

    def __setattrib__(self, name, value):
        self.__dict__[name] = value

    def __getattrib__(self, name):
        return self.__dict__[name]

name = input("Enter pet name: ")
animal_type = input("Enter pet type: ")
age = input("Enter pet age: ")


p = Pet(name, animal_type, age)
print(p)

p.name = "new_name"
p.animal_type = "cat"
p.age = "10"

print(p)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.