【发布时间】:2021-09-25 12:37:36
【问题描述】:
如果找不到国家/地区(例如,用户写错了城市),如何在终端上创建自定义输出?
默认输出是错误码:
raise exceptions.NotFoundError('无法找到资源') pyowm.commons.exceptions.NotFoundError: 找不到资源
我希望输出类似于:
抱歉,我无法找到您搜索的城市。 请重试...
这是我目前的代码:
import pyowm
owm = pyowm.OWM('api code')
weather = True
while weather == True:
weather_input = input('Enter the name of the city you want me to search - ')
mgr = owm.weather_manager()
weather_city1 = mgr.weather_at_place(weather_input)
w = weather_city1.weather
print('Weather in ' + weather_input + ' - ' + str(w.temperature('celsius')))
【问题讨论】:
-
试试
try:\n 'your whole code goes here' \nexcept pyowm.commons.exceptions.NotFoundError: print('Sorry, I was unable to find the city you searched for. Please try again...')
标签: python python-3.x openweathermap