记录#
- 今天是编码练习,从一个天气 api 获取信息并打印。
- 不太清楚 weathercode 的具体含意,所以小改了一下编码要求
- chatgpt 教给了我统计数字出现最多一项
CODE#
main.py#
import requests, json
from collections import Counter
timezone = "GMT"
latitude = 51.5002
longitude = -0.1262
result = requests.get(
f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&daily=weathercode,temperature_2m_max,temperature_2m_min&timezone={timezone.upper()}"
)
user = result.json()
#print(json.dumps(user, indent=2))
#print("++++")
number_count = Counter(user['daily']['weathercode'])
most_common_number, count = number_count.most_common(1)[0]
print(
f"天气情况大部分都是{most_common_number}\n最高气温:{max(user['daily']['temperature_2m_max'])}-----最低气温:{min(user['daily']['temperature_2m_min'])}"
)