🐍

Weather CLI App 🌤️

Intermediate
170 XP
75 min
Lesson Content

Weather CLI App 🌤️

Build a command-line weather app that fetches real weather data. Learn API integration and CLI development!

API Requests

import requests

def get_weather(city, api_key):
    url = f'https://api.weather.com/{city}'
    response = requests.get(url, params={'key': api_key})
    return response.json()

weather = get_weather('London', 'your_key')
print(f"Temperature: {weather['temp']}°C")
Example Code

Fetch weather data

# Simulate weather API
def get_weather(city):
    # In real app, use requests.get()
    weather_data = {
        'London': {'temp': 15, 'condition': 'Cloudy'},
        'Paris': {'temp': 20, 'condition': 'Sunny'}
    }
    return weather_data.get(city, {'temp': 0, 'condition': 'Unknown'})

city = 'London'
weather = get_weather(city)
print(f'{city}: {weather["temp"]}°C, {weather["condition"]}')

Expected Output:

Weather data for city
Study Tips
  • Read the theory content thoroughly before practicing
  • Review the example code to understand key concepts
  • Proceed to the Practice tab when you're ready to code