A
A
Andrey Romashev2021-10-24 13:04:41
Python
Andrey Romashev, 2021-10-24 13:04:41

Random telegram bot generates a result, and then is lazy and gives a repeat, how to fix it?

After starting the bot, it thinks to generate several random results, but then it starts to alternate them.

man = ['Парни', 'Огурцы', 'Холостяки', "Гришани", "Карамельки", "Мошенники", "Грибники" ]
place = [ "Баку", "Германии",  "Майами", "летающей тарелки", "девятки", "больницы",]
prilag = ['Клевые', 'Безбашенные', 'Вонючие']
such = ['вареники', 'конюхи', 'кореша']
team = ['Аргентина', 'Ложка']
random_team0 = random.choice(man) + ' из ' + random.choice(place)
random_team1 = random.choice(prilag) + ' ' + random.choice(such)
team_name = [random.choice(team), random_team0, random_team1]

@bot.message_handler(commands = ['team'])
def team(message):
    random.shuffle(man)
    random.shuffle(place)
    random.shuffle(such)
    random.shuffle(prilag)
    random.shuffle(team)
    bot.send_message(message.from_user.id, random.choice(team_name))


is there any way to make him think about each random?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-10-24
@isildurrr

random_team0 = random.choice(man) + ' of ' + random.choice(place)
random_team1 = random.choice(prilag) + ' ' + random.choice(such)
team_name = [random.choice(team), random_team0, random_team1 ]
This code is executed once when the bot is started, not every time team() is executed. Put it inside the team() subroutine.

J
jerwright, 2021-10-24
@jerwright

random_choice = ""
@bot.message_handler(commands = ['team'])
def team(message):
    local_random_choice = random.choice(team_name) #Рандомный элемент, который меняется после вызова команды
    global random_choice #Задаём глобальную переменную
    while random_choice == local_random_choice:
        local_random_choice = random.choice(team_name)
    random_choice = local_random_choice #После итераций замены рандомного элемента наконец меняем глобальную переменную
    bot.send_message(message.from_user.id, random_choice)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question