S
S
Studentka19962020-02-02 22:31:06
Python
Studentka1996, 2020-02-02 22:31:06

How to save! added values ​​to the list, after the function ends?

There are two functions start and contact, they have variables, the values ​​of which were received from the chat bot. I want to save these values ​​in one sheet, in order to use these values ​​elsewhere in the program. But, after the completion of the functions, I get None - that is, the return of an empty list (sheet). Values ​​are written / added and stored apparently only at the moment of execution of instructions inside functions. Please help

user_info = [ ]
@bot.message_handler(commands=['start'])
def start_msg(message):
       #Имя и фамилия пользователя
    user = f'{message.chat.first_name } {message.chat.last_name }'  
    bot.send_message(message.chat.id, f'''Здравствуйте, {user}. Для доступа ко всем функциям чат-бота, отправьте контактный телефон.''', reply_markup=interface.phone)
    user_info.append(user)
     print(user_info)
    
@bot.message_handler(content_types=['contact'])
def contact_msg(message):
    phone = message.contact.phone_number
    user_info.append(phone)
    print(user_info)
    bot.send_message(message.chat.id, f'''Ваш телефон: {phone }
        Для получения справки используйте команду /go_help или раздел "Помощь".''', reply_markup=interface.Main_menu)


I searched the Internet for available methods, but I did not find anything worthwhile to solve. Perhaps this is not possible :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Elvis, 2020-02-03
@Studentka1996

A python program is executed from top to bottom. You declared a variable, declared (but didn't execute!!!) two functions, and printed a variable that is still empty because there were still no function calls.

F
Filart97, 2020-02-03
@Filart97

Firstly, the very last line in the code that you have attached is executed only once in your application (unless you use multiprocessing), because this line is at the file (module) level and is executed when it is executed ( python file.py) or imported from him . Secondly, you already have print(user_info) calls inside functions, isn't there anything in the program output (or logs)? Thirdly, you can get all the information about the user right away inside the last function, where you get the phone. Just copy the line from the first function with the name.import * from file
And finally, I advise you to change the approach with which you want to use the result of these two functions in another part of the program. Argue for a long time. It is better to move all necessary actions from user_info inside one of these handlers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question