-1

When wanting to return a value with the return and wanting to use it later in a variable, nothing is returned to me.

#Actualizacion de informacion
def messageHandler(update: Update, context: CallbackContext):
    
    userName = update.effective_user['username']
    #text = update.message.text
    Usuario = userName
    ConsultarServer = ("Ingresa tu nombre de troncal correcta de lo contrario no se te regresara ninguna informacion.")

    if ConsultarSaldoSw2 in update.message.text:
        context.bot.send_message(chat_id=update.effective_chat.id, text="Ingresa tu nombre de troncal correcta de lo contrario no se te regresara ninguna informacion.")
        text2 = update.message.text
        return text2
    
   
    print (text2)
    
    if text2 in update.message.text:
        context.bot.send_message(chat_id=update.effective_chat.id, text="Ingresa el servidor sw o sw2")
        text3 = update.message.text
        return text3
    
    
    
    print (text3)
  • 1
    `return` ends the function. Since you return after setting `text2`, the rest of the code will never be executed. – Barmar Apr 21 '22 at 21:21
  • You do not have return under all circumstances, but only within two if loops. If none of both conditions is fulfilled, you have no active return statement in your function. So I expect you get a None in that case – Giovanni Tardini Apr 21 '22 at 21:21
  • 1
    And if the first `if` condition fails, you don't set `text2`, and the rest of the function will get an error because it tries to use an undefined variable. – Barmar Apr 21 '22 at 21:22
  • Does this answer your question? [How is returning the output of a function different from printing it?](https://stackoverflow.com/questions/750136/how-is-returning-the-output-of-a-function-different-from-printing-it) – ddejohn Apr 21 '22 at 21:22
  • 2
    "nothing is returned" isn't possible. Something will be returned (it may be `None`). So how do you call this function and what do you get back? – Matthias Apr 21 '22 at 21:22
  • could you give me examples please – Benito Barrios Ruiz Apr 21 '22 at 21:31

2 Answers2

1

If the function is exhausted, and it doesn't meet any of your conditions, it will return None by default. Consider this shortened example:

def messageHandler(update: Update, context: CallbackContext):

    if ConsultarSaldoSw2 in update.message.text:
        return text2
    
    if text2 in update.message.text:
        return text3
    
    # None of these if conditions are met, so nothing is returned

If you don't enter any of these if statements, you never return a value.

Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29
0

Return ends the function and does not print anything to the console. Also seems that they're endpoints for conditionals that may not be met.