Skip to content

Commit

Permalink
added weatherapp.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AFurqanHassan authored Oct 27, 2024
1 parent 045ec95 commit 8c47aca
Showing 1 changed file with 32 additions and 0 deletions.
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)

0 comments on commit 8c47aca

Please sign in to comment.