Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nik Zulfauzaan | WeatherForecast/ | Added the folder WeatherForecast … #371

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions WeatherForecast/README.md
Original file line number Diff line number Diff line change
@@ -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.
41 changes: 41 additions & 0 deletions WeatherForecast/weather.py
Original file line number Diff line number Diff line change
@@ -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()