
import requests

api_key = "8ce38f23e91ac10a31ab2f9ffdee3621"
city_name = input("Enter city name: ")
url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={api_key}"

print(url)

req = requests.get(url)
weather_data = req.json()

if weather_data["cod"] != "404":
    main = weather_data['main']
    current_temperature = round(main['temp'] - 273.15, 2)
    current_humidity = main['humidity']
    weather = weather_data['weather']
    weather_description = weather[0]['description']
    
    print("\nCurrent weather in", city_name)
    print(f"Temperature: {current_temperature} °C")
    print(f"Humidity: {current_humidity}%")
    print("Description:", weather_description)

    forecast_url = f"https://api.openweathermap.org/data/2.5/forecast?q={city_name}&appid={api_key}"
    forecast_req = requests.get(forecast_url)
    forecast_data = forecast_req.json()

    if forecast_data["cod"] != "404":
        print("\nWeather forecast for the next few days:")
        for item in forecast_data['list']:
            date = item['dt_txt']
            temperature = round(item['main']['temp'] - 273.15, 2)
            description = item['weather'][0]['description']
            print(f"Date: {date}, Temperature: {temperature} °C, Description: {description}")
    else:
        print("Failed to retrieve forecast data.")
else:
    print("City not found.")
