skip to Main Content

I have this code ⬇️ in a Telegram bot (whit telebot).
When I send /start it send a random element but It send always the same

v1 = "1111"
v2 = "ABCD"
v3 = "EFGH"
v4 = "XXXX"
v5 = "0000"

list = [v1, v2, v3, v4, v5]
abcd = random.choice(list)

@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
    bot.reply_to(message, abcd)

How can I solve it?

2

Answers


  1. You select a value only once. Replace bot.reply_to(message, abcd) with bot.reply_to(message, random.choice(list)) to get random result every time.

    Login or Signup to reply.
  2. You could use

    @bot.message_handler(commands=['help', 'start'])
    def send_welcome(message):
        bot.reply_to(message, random.choice(['1', '2', '3', '4']))
    

    I think it is better to use a smaller amount of variables and the variable name list is a built-in class
    also check if you have a
    random.seed()
    in your code

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search