Lazo Lab Sign up

App 4: Weather App with a Real API / Build the weather app

Lesson 6 of 10

Putting it together

Ask for a city, find its coordinates, fetch the weather and print it. Each step is its own small function.

Key points

  • One function per job
  • Check for None between steps
  • Loop so users can check many cities
def get_weather(lat, lon):
    r = requests.get("https://api.open-meteo.com/v1/forecast", timeout=10,
                     params={"latitude": lat, "longitude": lon, "current": "temperature_2m,wind_speed_10m"})
    r.raise_for_status()
    return r.json()["current"]

city = find_city(input("City: "))
if city:
    name, lat, lon = city
    now = get_weather(lat, lon)
    print(f"{name}: {now['temperature_2m']}°C, wind {now['wind_speed_10m']} km/h")
else:
    print("Couldn't find that city.")

Your turn

Combine your functions so the app works for any city.

Watch a video on thisOpens YouTube search results for “Putting it together” in a new tab

Quiz · +10 XP

Why split the app into find_city and get_weather?

Log in to save progress and earn XP.