D
D
Dozen Frozenballs2015-10-03 05:53:35
Python
Dozen Frozenballs, 2015-10-03 05:53:35

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

2 answer(s)
S
Seva Zhidkov, 2015-10-03
@Shamoi

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Вы установили местоположение и время.")

Moreover, if you have many users and you need to store the location for each user, then use, for example, Redis or another key value storage, even in files.

Q
qwescin, 2015-10-03
@qwescin

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 question

Ask a Question

731 491 924 answers to any question