Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 3dc45ac

Browse files
committed
feat: add logic for city not found in API.
1 parent 4fd7b68 commit 3dc45ac

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

projects/API Based Weather Report/API Based Weather Report.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def fetch_weather(api_key, location):
2525

2626

2727
# Function to write weather information to a text file
28-
def write_to_file(location, weather_data):
28+
def write_to_file(weather_data):
2929
try:
3030
# Opening the file "weatherinfo.txt" in write mode
3131
with open("weatherinfo.txt", "w+") as f:
@@ -114,8 +114,12 @@ def main():
114114

115115
# Checking if weather data was successfully fetched
116116
if weather_data:
117+
if weather_data["cod"] == "404":
118+
print("City not found.")
119+
return
120+
117121
# Writing weather information to file
118-
write_to_file(location, weather_data)
122+
write_to_file(weather_data)
119123

120124
# Printing weather information to console
121125
print("Current City : " + weather_data['name'] + ', ' +

projects/API Based Weather Report/Util_Functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def wind_degree_to_direction(str_wind_degree):
2424

2525
def unix_timestamp_to_localtime(str_unix_timestamp, str_timezone_offset_seconds):
2626
"""
27-
Convert wind degree to wind direction.
27+
Convert unix timestamp to localtime (for sunrise and sunset).
2828
2929
:param str_unix_timestamp: str, Unix timestamp (e.g., "1717715516")
3030
:param str_timezone_offset_seconds: str, timezone offset in second (e.g., "28800" represents UTC+8)

0 commit comments

Comments
 (0)