enricozz
enricozz

Reputation: 9

Python RANDOM always got same result

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?

Upvotes: 0

Views: 140

Answers (2)

error 1044
error 1044

Reputation: 109

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

Upvotes: 0

Ratery
Ratery

Reputation: 2917

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.

Upvotes: 3

Related Questions