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