Exercise: Classes

In the following exercises, you'll implement the __init__ and other methods of a class. Make sure you first read through the class doctests to see what instance variables and methods are expected to exist on each class.

Plant

Finish the implementation of Plant, a class that can be used by a plant nursery to track which plants they have in inventory and how many they have of each plant.

📺 Need help with this one? Watch a video walkthrough of the solution. Then try it out yourself and apply what you learned to the exercises after.

🧩 Or, for a different form of guidance, try a Parsons Puzzle version first.

class Plant: """A class used by a plant nursery to track what plants they have in inventory and how many they have of each plant. >>> milkweed = Plant("Narrow-leaf milkweed", "Asclepias fascicularis") >>> milkweed.common_name 'Narrow-leaf milkweed' >>> milkweed.latin_name 'Asclepias fascicularis' >>> milkweed.inventory 0 >>> milkweed.update_inventory(2) >>> milkweed.inventory 2 >>> milkweed.update_inventory(3) >>> milkweed.inventory 5 """ def __init__(self, common_name, latin_name): self.common_name = common_name # Fill out the rest of this function def update_inventory(self, amount): # Fill out this function

MoviePurchase

Finish the implementation of MoviePurchase, a class that represents movie purchases on YouTube, tracking the title and costs of each movie bought, plus the number of times watched.

🧩 For more guidance, try a Parsons Puzzle version first.

class MoviePurchase: """A class that represents movie purchases on YouTube, tracking the title and cost of each movie bought, as well as the number of times the movie is watched. >>> ponyo = MoviePurchase("Ponyo", 19.99) >>> ponyo.title 'Ponyo' >>> ponyo.cost 19.99 >>> ponyo.times_watched 0 >>> ponyo.watch() >>> ponyo.watch() >>> ponyo.times_watched 2 """ def __init__(self, title, cost): # Fill out this function def watch(self): # Fill out this function

Player

Finish the implementation of Player, a class that represents a player in a video game, tracking their name and health.

🧩 For more guidance, try a Parsons Puzzle version first.

class Player: """A class that represents a player in a video game, tracking their name and health. >>> player = Player("Mario") >>> player.name 'Mario' >>> player.health 100 >>> player.damage(10) >>> player.health 90 >>> player.boost(5) >>> player.health 95 """ def __init__(self, name): # YOUR CODE HERE def damage(self, amount): # YOUR CODE HERE def boost(self, amount): # YOUR CODE HERE

Clothing

Implement Clothing, a class that represents pieces of clothing in a closet, tracking their color, category, and clean/dirty state.

🧩 For more guidance, try a Parsons Puzzle version first.

class Clothing: """A class that represents pieces of clothing in a closet, tracking the color, category, and clean/dirty state. >>> blue_shirt = Clothing("shirt", "blue") >>> blue_shirt.category 'shirt' >>> blue_shirt.color 'blue' >>> blue_shirt.is_clean True >>> blue_shirt.wear() >>> blue_shirt.is_clean False >>> blue_shirt.clean() >>> blue_shirt.is_clean True """ # Write an __init__ method # Write the wear() method # Write the clean() method
➡️ Next up: More on classes