-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeatherFinder.py
67 lines (55 loc) · 2.09 KB
/
WeatherFinder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
API_KEY = ""
def get_weather(city, api_key):
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(url)
data = response.json()
return data
def get_weather_emoji(weather_code):
weather_emojis = {
"01d": "☀️", # clear sky (day)
"01n": "🌙", # clear sky (night)
"02d": "⛅️", # few clouds (day)
"02n": "🌥", # few clouds (night)
"03d": "🌥", # scattered clouds (day)
"03n": "🌥", # scattered clouds (night)
"04d": "☁️", # broken clouds (day)
"04n": "☁️", # broken clouds (night)
"09d": "🌧", # shower rain (day)
"09n": "🌧", # shower rain (night)
"10d": "🌧", # rain (day)
"10n": "🌧", # rain (night)
"11d": "⛈", # thunderstorm (day)
"11n": "⛈", # thunderstorm (night)
"13d": "❄️", # snow (day)
"13n": "❄️", # snow (night)
"50d": "🌫", # mist (day)
"50n": "🌫", # mist (night)
}
# Default to question mark if code not found
return weather_emojis.get(weather_code, "❓")
def display_weather(data):
city = data["name"]
weather = data["weather"][0]
description = weather["description"]
weather_code = weather["icon"]
temperature = data["main"]["temp"]
feels_like = data["main"]["feels_like"]
humidity = data["main"]["humidity"]
wind_speed = data["wind"]["speed"]
emoji = get_weather_emoji(weather_code)
print(f"Weather in {city}: {description} {emoji}")
print(f"Temperature: {temperature}°C (Feels like: {feels_like}°C)")
print(f"Humidity: {humidity}%")
print(f"Wind Speed: {wind_speed} m/s")
def main():
city = input("Enter city name: ")
api_key = API_KEY
print("Fetching weather data...")
weather_data = get_weather(city, api_key)
if weather_data["cod"] == 200:
display_weather(weather_data)
else:
print("Error fetching weather data. Please check your city name and API key.")
if __name__ == "__main__":
main()