Answer the question
In order to leave comments, you need to log in
After entering the wrong name in the pythone weather bot, an error pops up, what should I do about it?
After entering the wrong city name, the bot stops working, what should I do about it? I would be very grateful for your help
import pyowm
import telebot
owm = pyowm.OWM('***********', language = "ru")
bot = telebot.TeleBot("******** *******")
@bot.message_handler(content_types=['text'])
def send_echo(message):
observation = owm.weather_at_place( message.text )
w = observation.get_weather()
temp = w.get_temperature ('celsius')["temp"]
hum = w.get_humidity()
time = w.get_reference_time(timeformat='iso')
wind = w.get_wind()["speed"]
answer ="In the city" + message. text + "now" + w.
answer += "Temperature is now around " + str(temp) + "\n\n" + "\nWind speed: " + str(wind) + "m/s" + "\n" + "\nHumidity: " + str(hum) + "%" + "\n" + "\nTime: " + str(time) + "\n"
if temp < 11:
answer += "It's very cold now."
elif temp < 20:
answer += "It's cold now, better dress warmly."
else:
answer += "Temperature is OK!"
bot.send_message(message.chat.id, answer)
bot.polling( none_stop = True)
input()
After entering the wrong name, the following error pops up: raise api_response_error.NotFoundError('Unable to find the resource')
pyowm.exceptions. api_response_error.NotFoundError: The searched item was not found.
Reason: Unable to find the resource
Answer the question
In order to leave comments, you need to log in
Use the try - except construct.
Working code:
import telebot
import pyowm
owm = pyowm.OWM('key', language = "ru")
bot = telebot.TeleBot('token')
@bot.message_handler(content_types=['text'])
def send_echo(message):
try:
observation = owm.weather_at_place( message.text )
w = observation.get_weather()
temp = w.get_temperature('celsius')["temp"]
hum = w.get_humidity()
time = w.get_reference_time(timeformat='iso')
wind = w.get_wind()["speed"]
answer ="В городе " + message.text + " сейчас " + w.get_detailed_status() + "\n"
answer += "Температура сейчас в районе " + str(temp) + "\n\n" + "\nСкорость ветра: " + str(wind) + "м/с" + "\n" + "\nВлажность: " + str(hum) + "%" + "\n" + "\nВремя: " + str(time) + "\n"
if temp < 11:
answer += "Сейчас очень холодно."
elif temp < 20:
answer += "Сейчас прохладно, лучше одеться потеплее."
else:
answer += "Температура в норме!"
bot.send_message(message.chat.id, answer)
except:
bot.send_message(message.chat.id,'Ошибка! Город не найден.')
bot.polling( none_stop = True)
input()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question