简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM