-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday 7.py
More file actions
71 lines (55 loc) · 1.96 KB
/
day 7.py
File metadata and controls
71 lines (55 loc) · 1.96 KB
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
68
69
70
71
import requests
api_key = "1ab5202f1c0e93f4fa2cca4eeb164fe5"
base_url = "http://api.openweathermap.org/data/2.5/weather"
def get_weather_emoji(condition):
condition = condition.lower()
if "clear" in condition:
return "☀️"
elif "cloud" in condition:
return "☁️"
elif "rain" in condition:
return "🌧️"
elif "storm" in condition or "thunder" in condition:
return "⛈️"
elif "snow" in condition:
return "❄️"
elif "mist" in condition or "fog" in condition:
return "🌫️"
else:
return "🌍"
while True:
city = input("\nEnter city name (or 'exit' to quit): ").strip()
if city.lower() == "exit":
print("\nGoodbye 👋 Stay safe!")
break
if city == "":
print("⚠️ City name can't be empty.")
continue
params = {
"q": city,
"appid": api_key,
"units": "metric"
}
try:
response = requests.get(base_url, params=params)
if response.status_code == 200:
data = response.json()
temp = data["main"]["temp"]
weather = data["weather"][0]["description"]
humidity = data["main"]["humidity"]
pressure = data["main"]["pressure"]
wind_speed = data["wind"]["speed"]
emoji = get_weather_emoji(weather)
print("\n" + "=" * 35)
print(f" Weather Report for {city.title()} {emoji}")
print("=" * 35)
print(f"🌡️ Temperature : {temp}°C")
print(f"🌤️ Condition : {weather.title()}")
print(f"💧 Humidity : {humidity}%")
print(f"🧭 Pressure : {pressure} hPa")
print(f"🌬️ Wind Speed : {wind_speed} m/s")
print("=" * 35)
else:
print("❌ City not found. Try again.")
except requests.exceptions.RequestException:
print("🌐 Network error. Check internet connection.")