-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
045ec95
commit 8c47aca
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
Program's_Contributed_By_Contributors/Python_Programs/weatherapp.py
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,32 @@ | ||
import requests | ||
|
||
|
||
def get_weather(city, api_key): | ||
base_url = "http://api.weatherapi.com/v1/current.json" | ||
params = {'key': api_key, 'q': city} | ||
response = requests.get(base_url, params=params) | ||
data = response.json() | ||
return data | ||
|
||
|
||
def display_weather(data): | ||
if data.get("error") is not None: | ||
print("City not found.") | ||
return | ||
weather = data["current"]["condition"]["text"] | ||
temp = data["current"]["temp_c"] | ||
humidity = data["current"]["humidity"] | ||
dewpoint = data["current"]["dewpoint_c"] | ||
|
||
print(f"Weather in {city}:") | ||
print(f"Weather: {weather}") | ||
print(f"Temperature: {temp}°C") | ||
print(f"Humidity: {humidity}") | ||
print(f"Dew Point: {dewpoint}°C") | ||
|
||
|
||
if __name__ == "__main__": | ||
api_key = "WeatherAPI.com API key" | ||
city = input("Enter the city name: ") | ||
weather_data = get_weather(city, api_key) | ||
display_weather(weather_data) |