Ключевая ошибка 'Main' при попытке открыть учебник по Python openweathermap - PullRequest
0 голосов
/ 30 мая 2019

В настоящее время я пытаюсь выполнить руководство по настройке openweathermap через Python, но получаю KeyError, и мне было интересно, может ли кто-нибудь мне помочь.

Ошибка, которую я получаюis KeyError: 'main'

В реальном коде, который я вставил в свой API, но вынул его по понятным причинам.

api_key = ""


base_url = "http://api.openweathermap.org/data/2.5/weather?"


city_name = 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 = y["temp"] 

current_pressure = y["pressure"] 


current_humidiy = y["humidity"] 


z = x["weather"] 

weather_description = z[0]["description"] 


print(" Temperature (in kelvin unit) = " +
                str(current_temperature) + 
      "\n atmospheric pressure (in hPa unit) = " +
                str(current_pressure) +
      "\n humidity (in percentage) = " +
                str(current_humidiy) +
      "\n description = " +
                str(weather_description)) 

else: 
    print(" City Not Found ")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...