1

The instructions for this assignment: Write a class named Pet, which should have the following attributes:

  • __name
  • __animal_type
  • __age

It will then have the following methods:

  • set_name
  • set_animal_type
  • set_age
  • get_name
  • get_animal_type
  • get_age

Once this class has been written, write a program that creates an object of the class and prompts the user to enter the name, type, and age of the pet. The data should be stored as the object's attributes. Use the object's accessor methods to retrieve the pet's name, type, and age and display on the screen.

Here is my code:

class Pet(object):
    def __init__(self, name, animal_type, age):
        self.__name = name
        self.__animal_type = animal_type
        self.__age = age

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

    def set_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


# The main function
def main():
    # Create an object from the Pet class
    pets = Pet(name, animal_type, age)

    # Prompt user to enter name, type, and age of pet
    name = input('What is the name of the pet: ')
    animal_type = input('What type of pet is it: ')
    age = int(input('How old is your pet: '))
    print('This will be added to the records. ')
    print('Here is the data you entered:')
    print('Pet Name: ', pets.get_name)
    print('Animal Type: ', pets.get_animal_type)
    print('Age: ', pets.get_age)

main()

When i run the program it gives me this error: UnboundLocalError: local variable 'name' referenced before assignment

Brandon
  • 21
  • 2
  • 4
  • 8
  • Hint: when you say this in `main()`, `pets = Pet(name, animal_type, age)`, `name`, `animal_type` and `age` don't exist in that scope. You need to figure out a way to instantiate a `Pet` with default name, type, age, etc. – juanchopanza Sep 25 '14 at 06:01
  • getters and setters are not generally used in python. You can use [`@property`](https://docs.python.org/3/library/functions.html#property) decorator to allow a function to be called when accessing a property. See [here](https://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters). – Paul Rooney Jun 26 '19 at 03:53

3 Answers3

2

In the main() function, the Pet() class object needs arguments. So, you first need to provide the arguments and then input them into the Pet() class object.

class Pet:
    def __init__(self, name,animal_type,age):
        self.__name = name
        self.__animal_type = animal_type
        self.__age = age

    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_age(self):
        return self.__age

    def get_animal_tpe(self):
        return self.__animal_type


def main():
    pet_name = input('Please enter your pet\'\s name: ')
    pet_type = input('What animal is your pet?')
    pet_age = float(input('What is the age of your pet?'))

    pet_specs = Pet(pet_name,pet_type,pet_age)

    print('pet name is ', pet_specs.get_name())
    print('pet type is ', pet_specs.get_animal_tpe())
    print('pet age is ', pet_specs.get_age())

main()
abenol
  • 89
  • 9
1

This line has variables that are undefined-

pets = Pet(name, animal_type, age)
           ^^^^^^^^^^^^^^^^^^^^^^

This line should be after you have received all the inputs.

name = input('What is the name of the pet: ')
animal_type = input('What type of pet is it: ')
age = int(input('How old is your pet: '))
#The line is moved here -
pets = Pet(name, animal_type, age)
Kamehameha
  • 5,423
  • 1
  • 23
  • 28
1

You problem is you are creating the pet before you have asked the user for the pet's details, you need to move your "create the pet" line, after you have finished asking for input:

# The main function
def main():
    # Create an object from the Pet class
    # pets = Pet(name, animal_type, age) --------------------- move this
                                                               #  |
    # Prompt user to enter name, type, and age of pet          #  |
    name = input('What is the name of the pet: ')              #  |
    animal_type = input('What type of pet is it: ')            #  |
    age = int(input('How old is your pet: '))                  #  |
    pets = Pet(name, animal_type, age) # <---------------------- here
    print('This will be added to the records. ')
    print('Here is the data you entered:')
    print('Pet Name: ', pets.get_name)
    print('Animal Type: ', pets.get_animal_type)
    print('Age: ', pets.get_age)

main()
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thanks that took care of that. Receiving this type of output though: Pet Name: > Pet Type: > Age: >. I'm pretty sure my print statements are a little off – Brandon Sep 25 '14 at 06:12