Skip to content

Instantly share code, notes, and snippets.

@guestPK1986
Last active August 30, 2020 15:46
Show Gist options
  • Save guestPK1986/71feadfe6244206d0b30d7bee5010e99 to your computer and use it in GitHub Desktop.
Save guestPK1986/71feadfe6244206d0b30d7bee5010e99 to your computer and use it in GitHub Desktop.
python functions, classes
# Create a class Cat which will define name and age of cat
# Instantiate the Cat object with 3 cats
# Create a function that finds the oldest cat
# Print out: "The oldest cat is x years old.". x will be the oldest cat age by using the function
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
cat1 = Cat('Kitty',3)
cat2 = Cat('Foxy',2)
cat3 = Cat('Mimi',5)
def oldest(c1,c2,c3):
cats = [c1.age,c2.age,c3.age]
return max(cats)
print(f'The oldest cat is {oldest(cat1,cat2,cat3)} years old.')
#OR
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
cat1 = Cat('Kitty',3)
cat2 = Cat('Foxy',2)
cat3 = Cat('Mimi',5)
def oldest_cat(*args):
return max(args)
print(f'The oldest cat is {oldest_cat(cat1.age,cat2.age,cat3.age)} years old.')
#given the code below:
class Pets():
def __init__(self, animals):
self.animals = animals
def walk(self):
for animal in self.animals:
print(animal.walk())
class Cat():
def __init__(self, name, age):
self.name = name
self.age = age
def walk(self):
return f'{self.name} is just walking around'
class Simon(Cat):
def sing(self, sounds):
return f'{sounds}'
class Sally(Cat):
def sing(self, sounds):
return f'{sounds}'
#1 Add nother Cat
class Suzy(Cat):
def sing(self, sounds):
return f'{sounds}'
#2 Create a list of all of the pets (create 3 cat instances from the above)
catSimon = Simon('Simon', 4)
catSally = Sally('Sally', 21)
catSuzy = Suzy('Suzy', 1)
my_cats = [catSimon,catSally,catSuzy]
#3 Instantiate the Pet class with all your cats
my_pets = Pets(my_cats)
#4 Output all of the cats singing using the my_pets instance
my_pets.walk()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment