-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull Weather Forcasting App request #20 from i-OmSharma/Om4
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import requests | ||
|
||
def get_weather(api_key, city): | ||
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}" | ||
response = requests.get(url) | ||
data = response.json() | ||
return data | ||
|
||
def display_weather(data): | ||
print(data) # Add this line to print the entire API response | ||
|
||
if data["cod"] != "404": | ||
if "main" in data: | ||
main = data["main"] | ||
temperature = main["temp"] - 273.15 | ||
weather_desc = data["weather"][0]["description"] | ||
print(f"Temperature: {temperature:.2f}°C") | ||
print(f"Weather: {weather_desc}") | ||
else: | ||
print("Unexpected response format from the API. Please check the API documentation.") | ||
else: | ||
print("City not found. Please check the spelling.") | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
api_key = "YOUR_API_KEY" # Replace with your OpenWeatherMap API key | ||
city = input("Enter city name: ") | ||
weather_data = get_weather(api_key, city) | ||
display_weather(weather_data) |