Answer the question
In order to leave comments, you need to log in
How can you pass a value to a variable from a function?
How can you pass the value of a variable from a function? In the code example, the user writes a city in the message and this value must be passed.
Quite a noob question, but I've been scratching my head for several hours, but I still can't find a solution.
location = geolocator.geocode(city) #СЮДА
@bot.message_handler(func=lambda m: True)
def locate_and_time(message):
def city_time(message):
parts = message.split(', ')
return parts[0], int(parts[1])
def to_weather(): #определение города и времени
city, time = city_time(message)
return(city, time) #ОТСЮДА
bot.send_message(message.chat.id, "Отлично!\nВы установили местоположение и время.")
Answer the question
In order to leave comments, you need to log in
Try to do something like this:
@bot.message_handler(func=lambda m: True)
def locate_and_time(message):
def city_time(message):
parts = message.split(', ')
return parts[0], int(parts[1])
def to_weather(): #определение города и времени
global location
city, time = city_time(message)
location = geolocator.geocode(city)
return(city, time) #ОТСЮДА
bot.send_message(message.chat.id, "Отлично!\nВы установили местоположение и время.")
The easiest option is to use "global".
location = geolocator.geocode(city) #СЮДА
@bot.message_handler(func=lambda m: True)
def locate_and_time(message):
def city_time(message):
parts = message.split(', ')
return parts[0], int(parts[1])
def to_weather(): #определение города и времени
global city
city, time = city_time(message)
return(city, time) #ОТСЮДА
bot.send_message(message.chat.id, "Отлично!\nВы установили местоположение и время.")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question