American Young Coder (AYC)

AYC logo
Loading Events

« All Events

  • This event has passed.

5:00 PM – Python Object Oriented Programming – Sebastian

April 18, 2023 @ 5:00 pm - 6:00 pm

Today We Did
  1. Large introduction to object oriented programming
  2. Learned about classes, constructors, attributes, methods, and objects
  3. In case you need anything, feel free to email me at sebastian@ayclogic.com
Homework
  1. Name your homework APR18_oop_hw, please submit by next Monday.
  2. Homework:a) Read through pages 158 to 161b) Given the code that we created which creates a “Pet” class:

    – Create a method “fetch” which takes no parameters, and increments hunger_level by 1 (your dog gets hungry when it plays fetch!)

    – Add an attribute called “social_level”, and add it as a parameter to the constructor. This way, whoever creates their pet can set how social it is.

    – Add a method called “introduce” which takes one parameter, another pet! This method has a bit of logic so I’m going to break it down further:

    First, check the social level of your pet (this would be the attribute you just created). Second, check the social level of the pet which is being passed into the introduce function (this part is tricky, try your best to figure it out). Here is how the logic would look:

    If both social levels are greater than or equal to 5, then print “<name attribute> the <animal_type attribute> is now best friends with <parameter name> the <parameter animal_type>!”

    If both social levels are smaller than 5, print “<name attribute> the <animal_type attribute> upset with <parameter name> the <parameter animal_type>, rawr!”

    Here’s the code where we left off:

    class Pet:
        def __init__(self, name, age, animal_type):
            self.name = name
            self.age = age
            self.animal_type = animal_type
            self.hunger_level = 0
    
        def feed(self):
            self.hunger_level -= 1
            print(f"{self.name} has been fed, hunger level is {self.hunger_level}")
    
    
    dog = Pet("Fido", 3, "dog")

    Now, test this out by making a new pet object the way we made a dog, remember to pass in the new parameter to the constructor, and test out the introduce function.

Details

Date:
April 18, 2023
Time:
5:00 pm - 6:00 pm
Event Categories:
,
Verified by MonsterInsights