Exercise: Houseplant Care

Finish a houseplant app's Plant class, a Cactus subclass that rests in winter, a CareLog dataclass and a function that lists today's plants to water, and let pytest's tests check each one.

Journeyman · Python · OOP · Updated

By

Leaf & Sprout, a houseplant care app, knows its plants' names and schedules but not yet when any of them needs water. You'll write the code that does: a Plant class with its methods, a Cactus that follows its own winter rule, a CareLog dataclass that records each watering, and a function that lists the plants to water today. The tests make plants with real dates and check what your code gives back.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-python-houseplant-care, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:

cd ~/python-course
git clone https://github.com/ask-codey/exercise-python-houseplant-care.git

Git makes a folder, exercise-python-houseplant-care, with the exercise code inside. Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment, as Virtual Environments and pip explains. On Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt

On a Mac (zsh):

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

The prompt starts with (.venv) once the virtual environment is on.

The folder holds:

  • plants.py: the classes and the function, for you to finish.
  • care_today.py: prints Leaf & Sprout's plants, today's watering list and a care log with your code. Ready to use.
  • tests/test_plants.py: the tests, which make plants and check what your code gives back. You don't need to change it.
  • requirements.txt and pytest.ini: the package the tests need, and where pytest finds the tests and your code.

What to do

Plant's __init__ is written for you: it stores name, water_every (the days between waterings) and last_watered (a date). Every other method stops with a NotImplementedError that says Not built yet., and the docstring under each one says what it should do, with examples:

    def needs_water(self, today):
        """Return True when at least water_every days have passed since last_watered, and False when fewer have.

        (today - self.last_watered).days is the number of days between the two dates.

        fern = Plant("Fern", 3, date(2026, 5, 7))
        fern.needs_water(date(2026, 5, 9))    -> False  (2 days)
        fern.needs_water(date(2026, 5, 10))   -> True   (3 days: exactly enough counts)
        """
        raise NotImplementedError("Not built yet.")

Replace each raise NotImplementedError("Not built yet.") line with your own code:

  1. Plant: water(today) makes today the plant's last_watered; needs_water(today) returns True when at least water_every days have passed; and __str__ returns a line such as Fern: water every 3 days, last watered 2026-05-07.
  2. Cactus(Plant): Cactus(name, last_watered) sets itself up as a plant that goes 14 days between waterings, with super().__init__(). Its own needs_water(today) waits for 30 days in December, January and February, when a cactus rests, and follows Plant's rule the rest of the year.
  3. CareLog: a dataclass with plant already written. Add waterings, a list of dates that starts empty (a new list for each log), and notes, a string that defaults to "". Then record(day) adds a date to the end of waterings.
  4. plants_to_water(plants, today): returns the names of the plants that need water today, in the list's order.

Each method returns its answer, or changes the object's attributes; none of them prints anything.

Check your work

Run the tests from the exercise folder, with the virtual environment on:

pytest

At first all 21 tests fail. That's expected: each piece you finish turns its tests green. A failing test's message says what it did, what it expected and what came back, for example Plant('Fern', 3, May 7).needs_water(May 10) should give True, but it gave False.

To see your code at work, run python care_today.py.

Hints

  • Work an example through by hand first. A fern watered every 3 days, last watered on May 7, needs water on May 10: 3 days have passed, and exactly enough counts.
  • Two dates make a number of days. (today - self.last_watered).days is a whole number, ready to compare with water_every. Classes: Blueprints for Your Own Things shows it at work.
  • Let the parent do the usual work. The cactus's needs_water only decides what's different in winter, and super().needs_water(today) answers the rest of the year. Inheritance: One Class Built on Another overrides a method the same way.
  • A list field needs a factory. field(default_factory=list) gives each log a list of its own, as Dataclasses: Classes That Hold Data explains.
  • Ask each plant. plants_to_water doesn't need to know which plants are cacti: each one answers needs_water with its own class's rule.

When you're done

Every test is green: pytest's last line says 21 passed. And python care_today.py prints:

Leaf & Sprout: your plants on 2026-05-10
Fern: water every 3 days, last watered 2026-05-07
Pothos: water every 7 days, last watered 2026-05-01
Snake Plant: water every 14 days, last watered 2026-05-01
Barrel Cactus: water every 14 days, last watered 2026-04-20

Water today: ['Fern', 'Pothos', 'Barrel Cactus']

After watering the fern:
Fern: water every 3 days, last watered 2026-05-10
CareLog(plant='Fern', waterings=[datetime.date(2026, 5, 10)], notes='likes the bathroom')

Does a cactus watered on January 1 need water on January 20? False

Now look back at Cactus. If Leaf & Sprout changed the usual rule, say to remind people a day early, how many methods would you change? If the cactus hands the rest of the year to super().needs_water(today), the answer is one: Plant's.

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.

  1. On GitHub, click +, then New repository. Name it (for example exercise-python-houseplant-care), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-python-houseplant-care.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-python-houseplant-care, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

Classes let a program's own kinds of things carry their rules with them, and a subclass that changes only what's different keeps every shared rule in one place, where one fix reaches them all.

Key terms

Related