Skip to content

Instantly share code, notes, and snippets.

@s2t2
Last active August 24, 2025 18:52
Show Gist options
  • Save s2t2/27cb6dcf06970a9564445a9e991a0088 to your computer and use it in GitHub Desktop.
Save s2t2/27cb6dcf06970a9564445a9e991a0088 to your computer and use it in GitHub Desktop.
# resources:
# https://docs.python.org/3/tutorial/classes.html
# https://www.tutorialspoint.com/python/python_classes_objects.htm
# https://learnpythonthehardway.org/book/ex40.html
class Pet:
# expects to be instantiated using a dictionary like ...
# pet = Pet({'name':'Kirby', 'type':'Dog', 'age':'really old'})
def __init__(self, options):
self._name = options['name']
self._animal_type = options['type'] # e.g. 'Dog', 'Cat' and 'Bird'
self._age = options['age']
def get_name(self):
return self._name
def get_animal_type(self):
return self._animal_type
def get_age(self):
return self._age
def set_name(self, new_name):
self._name = new_name
def set_animal_type(self, new_type):
self._animal_type = new_type
def set_age(self, new_age):
self._age = new_age
# Once you have written the class, write a program that creates an object of the class and prompts the user to enter the name, type and age of his or her pet. This 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 this data on the screen.
print("----------------")
print("PETS APPLICATION")
print("-----------------")
animal_type = input("Please input the type of pet (e.g. 'Dog'): ")
name = input("Please input the pet's name (e.g. 'Ruffles'): ")
age = input("Please input the pet's age in years (e.g. '11'): ")
pet = Pet({'name': name, 'type': animal_type, 'age': age})
print(pet._animal_type, pet._name, pet._age)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment