Skip to content

Commit 11af307

Browse files
authored
Merge pull Weather Forcasting App request #20 from i-OmSharma/Om4
#6
2 parents c0cef7b + d6ade7f commit 11af307

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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)

0 commit comments

Comments
 (0)