A
A
artur_agishev2021-06-23 14:05:45
Python
artur_agishev, 2021-06-23 14:05:45

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

1 answer(s)
M
Mikhail Krostelev, 2021-06-23
@artur_agishev

Wouldn't it be easier to use random.shuffle() ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question