- This event has passed.
12 PM – Python OOP – Joel
June 29 @ 12:00 pm - 1:00 pm
Today we did:
- We learned about OOP concepts and how they work in Python.
Homework:
- Make a new file called June29_Animal.py. Make a new class called Animal. The class will have 4 attributes: name, species, age, and sound. It will also have 3 methods: make_sound, describe, and age_in_human_years:
- The make_sound method will have no parameters (except self). Inside the method, it will display: “<name> the <species> makes a <sound> sound”. Replace <name> with that animal’s name, and do the same thing for the rest.
- The describe method is simple with no parameters: simply display “<name> is a <age>-year-old <species>.
- The age_in_human_years method once again has no parameters. Inside, it checks if the animal’s species is a “cat”. If it is, then it will return the age of the animal * 15 (mutliplied by 15). Otherwise, if the species is “dog”, then it will return the age of the animal * 7 (multiplied by 7). Finally, else it will return the age of the animal * 4 (multiplied by 4).
- Now, outside of the class, make three animals and fill in their name, species, age, and sound. For example, you can make a dog named Fred, aged 7 and makes a ‘woof’ sound.
- Make an animals list and append those 3 animals you created into that list.
- Now, loop through the list, and call the make_sound method for each animal.
- Make another for loop and call the describe function for each animal in the list.
- Finally, make one last for loop, and call the animal’s age_in_human_years method and set that to a variable called human_years. Then, print “<name> is <human_years> human years old”.
If you have any questions, feel free to email me at joel@ayclogic.com.