Amazing-Python-Scripts

Форк
0
39 строк · 1.2 Кб
1
import requests
2

3

4
def get_temperature(json_data):
5
    temp_in_celcius = json_data['main']['temp']
6
    return temp_in_celcius
7

8

9
def get_weather_type(json_data):
10
    weather_type = json_data['weather'][0]['description']
11
    return weather_type
12

13

14
def get_wind_speed(json_data):
15
    wind_speed = json_data['wind']['speed']
16
    return wind_speed
17

18

19
def get_weather_data(json_data, city):
20
    description_of_weather = json_data['weather'][0]['description']
21
    weather_type = get_weather_type(json_data)
22
    temperature = get_temperature(json_data)
23
    wind_speed = get_wind_speed(json_data)
24
    weather_details = ''
25
    return weather_details + ("The weather in {} is currently {} with a temperature of {} degrees and wind speeds reaching {} km/ph".format(city, weather_type, temperature, wind_speed))
26

27

28
def main():
29
    api_address = 'https://api.openweathermap.org/data/2.5/weather?q=Sydney,au&appid=a10fd8a212e47edf8d946f26fb4cdef8&q='
30
    city = input("City Name : ")
31
    units_format = "&units=metric"
32
    final_url = api_address + city + units_format
33
    json_data = requests.get(final_url).json()
34
    weather_details = get_weather_data(json_data, city)
35
    # print formatted data
36
    print(weather_details)
37

38

39
main()
40

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.