Weather Assistant — Files, JSON & API Calls
In this assignment, you'll build a command-line Weather Assistant that fetches live weather data from the internet and saves it to your computer. This project ties together everything from Week 4:
requests libraryClick below to download all the files you need to get started:
This project uses a real, free weather API. Before you can run the program you need a key:
# Mac / Linux export OPENWEATHER_API_KEY="your_key_here" # Windows (Command Prompt) set OPENWEATHER_API_KEY=your_key_here
By completing this assignment, you'll be able to:
Your Weather Assistant must support all of the following menu options:
cities.json; do not allow
duplicates
cities.json; handle the case
where the city is not in the list
weather_history.json and show the 10 most
recent entries
Your program must also:
OPENWEATHER_API_KEY is not setThe scaffolding file has 15 TODO items covering these functions:
load_api_key() — read key from environment, exit if missingload_cities() — read favorites from cities.jsonsave_cities(cities) — write favorites list back to fileadd_city(cities, city_name) — add with duplicate checkremove_city(cities, city_name) — remove with not-found checkget_weather(city_name, api_key) — call the API, return JSON or Nonedisplay_weather(city_name, data) — format and print the resultsave_weather_history(city_name, data) — append entry with timestampshow_history() — print the last 10 entriesshow_favorites(cities) — print numbered favorites listget_weather_for_favorites(cities, api_key) — loop and fetch allshow_menu() — print the numbered menumain() — menu loop and option dispatchweather_assistant.py — your main program with TODO markers to guide youcheat_sheet.py — reference patterns for environment variables, JSON, requests, and
exception handling
pip install requests in your terminalget_weather() working and printing data before
building the rest
print(data) on the API result to see the
full dictionary structure before you start extracting values
cities.json and weather_history.json
in VS Code after running the program to verify they look correct
--- Weather Assistant --- 1. Get weather for a city 2. Add city to favorites 3. Remove city from favorites 4. Get weather for all favorites 5. View weather history 6. Show favorite cities 7. Quit Choose an option: 1 Enter city name: London --- Weather for London --- Temperature : 54°F Condition : overcast clouds Humidity : 81% Wind Speed : 9.2 mph Choose an option: 2 Enter city name to add: London London added to favorites! Choose an option: 5 Recent weather history: 2026-02-22 14:35 | London | 54°F | overcast clouds