Jan-07-2021, 04:41 PM
(Jan-07-2021, 03:17 PM)ndc85430 Wrote: Why the rubbish variable names x, y and z instead of more meaningful ones like you have for other things?
like this
import requests, json api_key = "2eacaa2c66e011c77935d17f0b7a72ed" base_url = "http://api.openweathermap.org/data/2.5/weather?" city_name = "Berlin" ###input("Enter city name : ") complete_url = base_url + "appid=" + api_key + "&q=" + city_name response = requests.get(complete_url) x = response.json() if x["cod"] != "404": y = x["main"] current_temperature = f'{(float(y["temp"]) - 273.15):.1f}°C' current_pressure = f'{y["pressure"]} hPa' current_humidiy = f'{y["humidity"]}%' z = x["weather"] weather_description = z[0]["description"] print(f'Weather in {city_name}:\n\ Temperature: {current_temperature}\n\ atmospheric: {current_pressure}\n\ humidity: {current_humidiy}\n\ description: {weather_description}') else: print(" City Not Found ")
Output:Weather in Berlin:
Temperature: 1.8°C
atmospheric: 1010 hPa
humidity: 80%
description: light snow