-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
77 lines (68 loc) · 2.4 KB
/
weather.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
68
69
70
71
72
73
74
75
76
77
import requests
import time
API_KEY = 'd6c98de467c64de5a1050158240301'
favorite_cities = [] # list for favorite cities
def get_weather(city):
url = f'https://api.weatherapi.com/v1/current.json?key={API_KEY}&q={city}'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(f"Weather in {city}: {data['current']['condition']['text']}, "
f"Temperature: {data['current']['temp_c']}°C")
else:
print(f"Failed to fetch weather for {city}")
def add_favorite(city):
favorite_cities.append(city)
print(f"Added {city} to favorites.")
for city in favorite_cities:
print(city)
def remove_favorite(city):
if city in favorite_cities:
favorite_cities.remove(city)
print(f"Removed {city} from favorites.")
else:
print(f"{city} is not in favorites.")
def update_favorite(old_city, new_city):
if old_city in favorite_cities:
index = favorite_cities.index(old_city)
favorite_cities[index] = new_city
print(f"Updated {old_city} to {new_city} in favorites.")
else:
print(f"{old_city} is not in favorites.")
def list_favorites():
if favorite_cities:
print("Favorite Cities:")
for city in favorite_cities:
print(city)
else:
print("No favorite cities added yet.")
if __name__ == '__main__':
while True:
print("OPTIONS")
print("1.Check weather by city name")
print("2.Add a favourite city")
print("3.Remove a favourite city")
print("4.List favourite cities")
print("5.Auto refresh weather")
print("6.Exit")
choice=int(input("Enter your choice:"))
if choice== 1:
city=input("Enter city name")
get_weather(city)
elif choice==2:
city=input("Enter a city name to be added in favorites")
add_favorite(city)
elif choice==3:
city=input("Enter a city name to be removed from favorites")
remove_favorite(city)
elif choice==4:
list_favorites()
elif choice==5:
city=input("Enter a city name to check the auto refreshed weather")
while True:
get_weather(city) #refresh
time.sleep(15) # Simulating 15 seconds delay
elif choice==6:
break
else:
print("Invalid input")