Answer the question
In order to leave comments, you need to log in
Why doesn't sorting work in python telegram bot?
This method should sort the elements of the list in random order, but for some reason this does not work in the bot. How to fix?
import telebot
import config
import random
from telebot import types
bot = telebot.TeleBot(config.TOKEN)
tasks = []
# random sorting of the array
def random_sorting():
if len(tasks) > 2:
for i in range(len(tasks) - 1, 2):
rnd = random.randint(0, i-1)
tasks[i], tasks[rnd] = tasks[rnd], tasks[i]
@bot.message_handler(content_types=['text'])
def prioritization(message):
if message.chat.type == 'private':
tasks.append(str(message.text))
random_sorting()
bot.send_message(message.chat.id, '\n'.join(tasks))
# run
if __name__ == '__main__':
bot.polling(none_stop=True)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question