App 4: Weather App with a Real API / Build the weather app
Lesson 5 of 10
Finding a city
People type city names, not coordinates. Open-Meteo's geocoding API turns a name into latitude and longitude.
Key points
- Geocoding: name to coordinates
- Take the first result
- Handle cities that aren't found
def find_city(name):
r = requests.get("https://geocoding-api.open-meteo.com/v1/search",
params={"name": name, "count": 1}, timeout=10)
r.raise_for_status()
results = r.json().get("results")
if not results:
return None
c = results[0]
return c["name"], c["latitude"], c["longitude"]Your turn
Write find_city() and test it with Kuwait, London and a made-up name.
Quiz · +10 XP
What should happen if the city isn't found?
Log in to save progress and earn XP.