File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ import requests
2+
3+ def get_weather (api_key , city ):
4+ url = f"http://api.openweathermap.org/data/2.5/weather?q={ city } &appid={ api_key } "
5+ response = requests .get (url )
6+ data = response .json ()
7+ return data
8+
9+ def display_weather (data ):
10+ print (data ) # Add this line to print the entire API response
11+
12+ if data ["cod" ] != "404" :
13+ if "main" in data :
14+ main = data ["main" ]
15+ temperature = main ["temp" ] - 273.15
16+ weather_desc = data ["weather" ][0 ]["description" ]
17+ print (f"Temperature: { temperature :.2f} °C" )
18+ print (f"Weather: { weather_desc } " )
19+ else :
20+ print ("Unexpected response format from the API. Please check the API documentation." )
21+ else :
22+ print ("City not found. Please check the spelling." )
23+
24+
25+
26+ if __name__ == "__main__" :
27+ api_key = "YOUR_API_KEY" # Replace with your OpenWeatherMap API key
28+ city = input ("Enter city name: " )
29+ weather_data = get_weather (api_key , city )
30+ display_weather (weather_data )
You can’t perform that action at this time.
0 commit comments