''''''
import requests
while True:
cityname=input('请输入你要查询天气城市的名字:')
url='http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'%cityname
response=requests.get(url)
result=response.json()
if result['error'] != '0':
cityname=input('您查询的城市有误或不存在,请重新输入:')
break
#获得字典result中的列表results
results=result['results']
#获得列表results中的字典,命名为city_message
city_message=results[0]
def query_daily_weather():
#获得字典city_message中城市名字
cityname=city_message['currentCity']
print('* 查询城市:%s'%cityname)
#获得字典city_message中pm25的值
pm25=city_message['pm25']
print('* pm25:%s'%pm25)
pm25=int(pm25)
#判断污染指数
if 0<=pm25<35:
print('* 污染指数:优')
elif 35<=pm25<75:
print('* 污染指数:良')
elif 75<=pm25<115:
print('* 污染指数:轻度污染')
elif 115<=pm25<150:
print('* 污染指数:中度污染')
elif 150<=pm25<250:
print('* 污染指数:重度污染')
elif 250<=pm25:
print('* 污染指数:严重污染')
# 获得字典city_message中未来三天的天气状况
weather_data = city_message['weather_data']
# 获得实时温度
now_temperature = weather_data[0]['date']
print('* 当前温度:%s'%now_temperature)
# 获得风向
wind=weather_data[0]['wind']
print('* 风向:%s'%wind)
# 获得天气
weather=weather_data[0]['weather']
print('* 天气:%s'%weather)
# 获得温度
temperature=weather_data[0]['temperature']
print('* 温度:%s'%temperature)
def query_three_day_weather():
print('***************%s未来三天天气预报******************'%cityname)
for x in range(1,4):
# 获得字典city_message中未来三天的天气状况
weather_data = city_message['weather_data']
# 获得实时温度
now_temperature = weather_data[x]['date']
print('* 日期: %s' % now_temperature)
# 获得风向
wind = weather_data[x]['wind']
print('* 风向:%s' % wind)
# 获得天气
weather = weather_data[x]['weather']
print('* 天气:%s' % weather)
# 获得温度
temperature = weather_data[x]['temperature']
print('* 温度:%s' % temperature)
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
while True:
print('* 欢迎使用天气查询工具 *')
print('* 1,查询实时天气 *')
print('* 2,查询未来三天天气 *')
print('* 0,退出程序 *')
select=int(input('请选择您的操作:'))
while select<0 or select>2:
select=int(input('输入有误,请重新输入:'))
if select == 1:
query_daily_weather()
elif select == 2:
query_three_day_weather()
elif select == 0:
print('退出程序')
天气查询工具
最新推荐文章于 2024-05-21 10:01:33 发布