19/April - Practing Object-oriented Python

python gif

Class Curriculum

Section content Expected time (mins) Pre - Requirements
Lesson Goals 5 minutes
Recap & questions from last class 10 minutes
Exercises explanation 10 minutes
Exercises in groups 30 minutes
Break 10 minutes
Exercises in groups 40 minutes
Check-out 10 minutes

0. Lesson Goals

  • Recap content and activity from last class
  • Complete object oriented python exercises

1. Check-In

  • What was particularly challenging last class? Are there any remaining questions from last class?

2. Recap

 1class Pet:
 2  def __init__(self, name):
 3    self.name = name
 4
 5  def hello(self):
 6    print("Hello my name is " + self.name)
 7
 8class Cat(Pet):
 9  def meow(self):
10    print("Meeeeeooooww! I am a cat!")
11
12cat_one = Cat("maya")
13cat_one.hello()
14cat_one.meow()
15
16# In groups, we created:
17
18# cats with ages
19cat_one = Cat("maya", 5)
20cat_one.age() # 5
21
22# we got a list of many different animals and made the ones with even ages say hello
23
24# array called animals
25animals = [cat_one, cat_two, dog_one] # etc
26for animal in animals:
27  if animal.age() % 2 == 0:
28    animal.hello()
29
30# recap on dictionaries
31amount_of_pets_you_have = {'Cats' : 1, 'Dogs' : 2, 'Rabbit' : 1}
32amount_of_pets_you_have['Cats'] # 1

3. Exercises (Card Game)

In groups, let's start building a card game in Python. (While we won't implement an entire game in this activity, implementing a Blackjack, Poker, or other game-player could be a fun project!)

The goal will be to describe the real-world situation of a deck of cards using Python code. This is meant to be challenging; Google is your friend here, use it to look up how to do things and to help you understand the material!

Example solutions to parts 1-3 will be posted after class :)

  1. If you aren't sure what a deck of cards is, feel free to check out this Wikipedia page or chat with your group :). The rest of this exercise assumes a standard 52-card deck (French deck), but feel free to use a different style if you wish.

  2. Define a Card class.

    • What properties should this class have?
    • Include an object method that displays the card. (Hint: you can print the properties of the card.)
    • Test your implementation by creating and displaying a few cards (ex. Ace of Spades, 4 of Hearts, 10 of Diamonds, Queen of Clubs...)