Search code examples
pythonshufflesentencepy-telegram-bot-api

Shuffle sentences pytelegrambotapi python


I am writing a bot on Python by telegrambotapi. Bot should send a random word from 2 lists.

My main question is, when sending a word from two lists, I want it to randomly shuffle the order of randomised words.

Here is a part of my code:

import telebot
def start(message):
 guilty = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7', 'name8']
 guiltless = ['nam1', 'nam2', 'nam3', 'nam4', 'nam5', 'nam6', 'nam7', 'nam8', 'nam9', 'nam10']
    bot.send_message (message.chat.id, str(random.choice(guilty) + str(random.choice(guiltless))
bot.polling()

When sending a message, I want it randomly shuffle the order of the sentence.


Solution

  • If you want to randomly change the order of the 'guilty' and 'guiltless' messages, you can shuffle the outputs of your choices:

    message_parts = [random.choice(guilty), random.choice(guiltless)]
    random.shuffle(message_parts)
    bot.send_message (' '.join(message_parts))
    

    I separated them with a space ' ' using join, you may change it with whatever you want.