Exercise: Camping Trip Forecast

Write the four functions behind a camping trip's forecast, finding the forecast's address in the Weather Service's answer, picking out the weekend, planning the packing and falling back to a saved copy, and let pytest's tests check them offline with saved answers and stand-in fetches.

Journeyman · Python · REST API · Web Development · Updated

By

The weekend at Pine Ridge Campground is almost here, and the group chat has one question: what do we pack? You'll write the four functions that answer it from the National Weather Service's forecast: one finds the forecast's address in the point's answer, one picks out Saturday and Sunday, one turns them into a packing list, and one gets the forecast live or, when there's no signal, from the saved copy. The tests run offline, on the Weather Service's real answers saved in the exercise, so they give the same result on any computer, any day.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-python-camping-forecast, 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-camping-forecast.git

Git makes a folder, exercise-python-camping-forecast, 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. 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

This time requirements.txt installs requests as well as pytest. The folder holds:

  • forecast.py: fetch_json, ready to use, and the four functions for you to finish.
  • trip.py: prints the weekend's forecast and packing list with your functions. Ready to use.
  • saved/points.json and saved/forecast.json: the Weather Service's real answers for the campground, saved on 2026-11-17, which the tests read.
  • saved/forecast-copy.json: the saved copy that get_forecast falls back to.
  • tests/test_forecast.py: the tests. You don't need to change it.

What to do

Open saved/points.json and saved/forecast.json first, and find the values you'll need: the forecast address, the periods, and inside one period its name, temperature, probabilityOfPrecipitation and shortForecast. Then, in forecast.py, replace each raise NotImplementedError("Not built yet.") with your own code. Each function's docstring has the details and examples:

def forecast_url(points_json):
    """Return the forecast's address from the Weather Service's points answer.

    It's the "forecast" value inside the answer's "properties".

    With saved/points.json:
    forecast_url(points) -> "https://api.weather.gov/gridpoints/PQR/107,74/forecast"
    """
    raise NotImplementedError("Not built yet.")
  1. forecast_url(points_json) returns the forecast's address from the points answer.
  2. weekend_periods(forecast_json) returns the periods named Saturday, Saturday Night, Sunday and Sunday Night, in order, each as a small dictionary with "name", "temperature", "rain_chance" and "summary". A rain chance the Weather Service sends as null (None in Python) becomes 0.
  3. packing_list(periods) returns ["tent", "headlamp", "water bottles"], then adds "tarp" when any period's rain chance is 50 or more, and "warm sleeping bag" when any period's temperature is below 40°F.
  4. get_forecast(fetch, saved_path) asks through fetch for the point's answer and then the forecast. When both work, it saves the forecast to saved_path with today's date and returns the forecast and "live". When fetch raises a requests.exceptions.RequestException, it returns the saved copy's forecast and "saved copy from " with the copy's date.

get_forecast never calls requests.get itself: it uses whatever fetch it's given. The real one is fetch_json; the tests hand in stand-ins that answer from the saved files, or fail with no connection, a timeout or a 500 error.

Check your work

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

pytest

At first all 19 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it called, what it expected and what came back, for example packing_list([Sunday 45°F, 50% rain]) should return ['tent', 'headlamp', 'water bottles', 'tarp'], but it returned ['tent', 'headlamp', 'water bottles'].

To see your functions at work, run python trip.py --offline, which uses the saved copy. Then run python trip.py for this weekend's real forecast, with the network on.

Hints

  • Read the saved answers first. Every key your code follows is in saved/points.json and saved/forecast.json. What a Web API Is shows how to follow keys from the outside in.
  • in checks a list. period["name"] in WEEKEND is True for the four weekend names and False for the rest.
  • Remember a match for after the loop. For "any period", a variable that starts as False and becomes True when a period matches does the job.
  • Both requests go in the try block. If either one fails, the saved copy is the answer. Saved Copies: Working Offline shows the shape, with a fetch that's handed in.
  • Watch the boundaries. 50% rain packs the tarp and 49% doesn't; 39°F packs the warm sleeping bag and 40°F doesn't. The tests check both sides.

When you're done

Every test is green: pytest's last line says 19 passed. And python trip.py --offline prints:

Pine Ridge Campground: this weekend (saved copy from 2026-11-17)
  Saturday: 43°F, 74% chance of rain, Light Rain Likely
  Saturday Night: 38°F, 74% chance of rain, Light Rain Likely
  Sunday: 45°F, 50% chance of rain, Chance Light Rain
  Sunday Night: 38°F, 40% chance of rain, Chance Light Rain
Pack: tent, headlamp, water bottles, tarp, warm sleeping bag

python trip.py prints the same lines for this weekend's real forecast, marked live, and saves it as the new copy. Now look back at get_forecast: if the Weather Service changed its forecast's address tomorrow, which of your functions would notice, and which tests would still pass?

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-camping-forecast), 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-camping-forecast.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-camping-forecast, 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

Real programs depend on services they don't control. Keeping the asking in one handed-in function, the reading in small functions that work on plain data, and a saved copy for the bad days is how a program that calls a web API stays useful and testable.

Key terms

Related