Displaying Appropriate Results
Lesson 10 of 10 in Coddy's API in Python course.
Challenge
MediumNow that you have successfully sent GET requests to an API server, let's display the specific results that we want by accessing nested dictionary values.
Instructions:
- Send a GET request to:
https://wttr.in/London?format=j1 - Save the
requests.get()object to a variable calledresponse - Save
response.json()to a variable calleddata - Access the current conditions, which are nested inside
data["current_condition"][0], and save them to a variable calledcurrent - Save
current["temp_C"]to a variable calledtemperature - Save
current["windspeedKmph"]to a variable calledwind - Create a variable called
cityand set it to"London" - Print all three variables in a formatted message
Try it yourself
import requests
# Step 1: Send GET request
response = # Your code here
# Step 2: Parse JSON response
data = # Your code here
# Step 3: Extract specific data
temperature = # Your code here
wind = # Your code here
city = # Your code here
# Step 4: Display results
print(f"City: {city}")
print(f"Temperature: {temperature}")
print(f"Wind: {wind}")All lessons in API in Python
3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results