From 0feceecd2e34b8539fd7c2b60a20db9620fa2452 Mon Sep 17 00:00:00 2001 From: Nik Zulfauzaan Date: Fri, 16 Feb 2024 10:38:44 +0800 Subject: [PATCH] Nik Zulfauzaan | WeatherForecast/ | Added the folder WeatherForecast that includes README.md and weather.py files --- WeatherForecast/README.md | 12 +++++++++++ WeatherForecast/weather.py | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 WeatherForecast/README.md create mode 100644 WeatherForecast/weather.py diff --git a/WeatherForecast/README.md b/WeatherForecast/README.md new file mode 100644 index 00000000..ccebb9a9 --- /dev/null +++ b/WeatherForecast/README.md @@ -0,0 +1,12 @@ +Project Title: Minimalistic Weather Forecast + +Purpose: +This project aims to provide users with a minimalistic weather forecast tool that retrieves current weather conditions and forecasts for a given location. + +Key Features: + +1. Location Input: Users can input their location (city name or zip code) through the command line interface. +2. Weather Retrieval: Utilize a weather API (such as OpenWeatherMap API) to fetch current weather conditions and forecasts for the specified location. +3. Display: Present the weather information in a clean and concise format, including details such as temperature, humidity, wind speed, and weather description. +4. Error Handling: Handle potential errors gracefully, such as invalid location input or network connectivity issues. +5. Unit Conversion: Provide an option for users to choose between metric and imperial units for temperature and wind speed. \ No newline at end of file diff --git a/WeatherForecast/weather.py b/WeatherForecast/weather.py new file mode 100644 index 00000000..b3698a03 --- /dev/null +++ b/WeatherForecast/weather.py @@ -0,0 +1,41 @@ +import requests + +def get_weather(location, units): + api_key = "YOUR_API_KEY" # Replace with your API key from OpenWeatherMap + url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units={units}" + response = requests.get(url) + if response.status_code == 200: + data = response.json() + return data + else: + print("Error fetching weather data.") + return None + +def display_weather(weather_data, units): + if weather_data: + print("Weather Forecast:") + print(f"Location: {weather_data['name']}, {weather_data['sys']['country']}") + if units == 'metric': + print(f"Temperature: {weather_data['main']['temp']}°C") + print(f"Wind Speed: {weather_data['wind']['speed']} m/s") + elif units == 'imperial': + print(f"Temperature: {weather_data['main']['temp']}°F") + print(f"Wind Speed: {weather_data['wind']['speed']} mph") + else: + print("Invalid unit selection.") + print(f"Humidity: {weather_data['main']['humidity']}%") + print(f"Weather: {weather_data['weather'][0]['description']}") + else: + print("Weather data unavailable.") + +def main(): + location = input("Enter city name or zip code: ") + units = input("Enter preferred units (metric/imperial): ").lower() + while units not in ['metric', 'imperial']: + print("Invalid units. Please enter 'metric' or 'imperial'.") + units = input("Enter preferred units (metric/imperial): ").lower() + weather_data = get_weather(location, units) + display_weather(weather_data, units) + +if __name__ == "__main__": + main() \ No newline at end of file