You are building a real-time weather reporting tool in Python! This program will ask the user for a city, connect to the internet, call a weather service API, and display the current weather for that location using clean, formatted output.
In this lab, you’ll explore real-world programming concepts like:
HTTP requests – how programs get information from the web
APIs (Application Programming Interfaces) – how programs talk to each other
JSON parsing – how to read data returned from a website
Error handling – how to avoid crashes and give helpful feedback
User input – customizing output for different cities or ZIP codes
You’ll also learn how to install external libraries in Python, something real developers do all the time.
You're about to write a Python program that connects to a real weather website (OpenWeatherMap) to get live weather data for any city. But here's the twist:
You’re not using a normal browser to get that data—you’re asking for it through code. And websites don’t like to just give away unlimited information to every robot or random program on the internet. That’s where an API key comes in.
An API key is like your secret password that tells the weather website: “Hey! I’m a real person, and I’m allowed to ask for data.”
It tells the OpenWeatherMap servers: “Hey, I’m a registered user. I’m allowed to ask for this data.”
Just like you wouldn’t let random strangers walk into your school and take all the Chromebooks, weather websites don’t let just any random Python script pull live data unless it shows its ID.
Websites like OpenWeatherMap use API keys to:
✅ Track who is using their data
🚦 Limit the number of requests per user (to avoid overloading their servers)
🔒 Prevent abuse, spam, or hacking attempts
💬 Give better service to real users
Go to https://openweathermap.org/api
Create a free account
Click on "Current Weather Data"
Copy your API Key (a long mix of numbers and letters)
You’ll paste your API key into your Python program like this:
api_key = "YOUR_API_KEY_HERE"
pip is Python’s package installer. Think of it like the App Store, but for Python libraries.
It lets you add tools that aren’t built into Python by default.
For this exercise, we'll use pip to install requests
Python doesn’t come with built-in tools for web communication, so we’ll use the requests library to send a web request to the weather API.
Open a terminal or command prompt
Type this: pip install requests
This downloads and installs the requests library onto your computer.
import brings in tools we want to use.
requests is what we’ll use to send a message to the weather website.
json lets us understand and organize the response we get back.
We’re using input() to let the user type the name of their city.
This will be sent to the weather API to get the specific city’s data.
api_key = "YOUR_API_KEY_HERE"
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
api_key is your secret code from OpenWeatherMap.
url is the full website address we’re calling. We’re adding:
q={city} → This puts the user's city into the URL
appid={api_key} → Adds our API key to prove we have access
units=metric → Makes the temperature show in Celsius (use imperial for Fahrenheit)
requests.get(url) sends a request to the weather server.
.json() turns the response into a Python dictionary.
We use keys like ["main"]["temp"] to get the exact info we want.
try and except protect your code. If something goes wrong (e.g., no internet), the program won’t crash—it prints a helpful message instead.
Think of API documentation as the user manual for developers. For OpenWeatherMap, here’s the official page for current weather data:
On this page, you’ll find:
The base URL to use
Examples of how to build your API request
A sample JSON response, showing all the fields it sends back
This tells you:
You get weather, main, wind, name, etc.
Inside main are things like temp and humidity
weather is a list, and you access the first item with [0]
✅ This uses json.dumps() to pretty-print the whole JSON object.
You can look through the output and say:
Oh, I see data["main"]["temp"]
Or data["weather"][0]["description"]
This is how pros learn new APIs—poke around and print it!
print(data.keys())
This gives you a quick look at the top-level categories inside your JSON. You can then drill down.
print(data["main"].keys())
Shows: dict_keys(['temp', 'feels_like', 'temp_min', 'temp_max', 'pressure', 'humidity'])
NOTE: Data is the json info you get back! That must be defined first!
Goal: Allow the user to input any city name.
Modify the code so the city isn’t hardcoded but comes from the input() function.
Example: "New York", "Tokyo", "Rome"
Goal: Display the weather data using clear labels and fun emoji.
Goal: Ask the user if they want Celsius or Fahrenheit and change the API accordingly.
Use units=metric or units=imperial based on their input.
Add °C or °F to the display.
Goal: Add basic error handling using try and except.
Catch bad API requests and missing cities.
Print messages like:
Goal: Let the user enter ZIP codes (12345) or cities with country codes (London,UK).
Update the q= parameter in your API URL accordingly.
Goal: Display additional weather info like:
feels_like temperature
pressure
visibility
sunrise/sunset time (convert from UNIX timestamp!)
Goal: Write a function that converts conditions into your own categories:
"clear sky" → 😎
"light rain" → ☔
"snow" → ❄️
Or make your own creative rating system
Goal: Save each city’s weather report to a .txt file.
Use open("weather_log.txt", "a") and write() the output.
Bonus: Include a timestamp using the datetime module.