编写递归随机变量的更好方法

Better way to write recursive random variables

我知道这真的很糟糕,但它确实有效,而且这是我目前所知道的完成这项工作的唯一方法。所以我正在寻找一种 "better" 的实践方式来写这个。

基本上它显示了 3 个图像插槽,这 3 个插槽使用 SLOT_PATTERN 中列出的随机图像更改图像。使用 edit_message 更改图像以编辑之前存在的 3 个插槽图像,将它们替换为给定列表中的随机图像。

import discord, asyncio, time, random

client = discord.Client()

@client.event
async def on_message(message):

    SLOT_PATTERN = [':four_leaf_clover:', ':moneybag:', ':cherries:', ':lemon:', ':grapes:', ':poop:', ':bell:', ':chocolate_bar:', ':watermelon:', ':six:', ':seven:', ':crown:']

    wheel_pick1 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick2 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick3 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick4 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick5 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick6 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick7 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick8 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick9 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick10 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick11 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick12 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick13 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick14 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick15 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick16 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick17 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick18 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick19 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick20 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick21 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick22 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick23 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick24 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick25 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick26 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick27 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick28 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick29 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]
    wheel_pick30 = SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]

    if message.content.lower().startswith("!slots"):
        slot1 = await client.send_message(message.channel, '%s %s %s' % (wheel_pick1, wheel_pick2, wheel_pick3))
        time.sleep(0.25)
        slot2 = await client.edit_message(slot1, '%s %s %s' % (wheel_pick4, wheel_pick5, wheel_pick6))
        time.sleep(0.25)
        slot3 = await client.edit_message(slot2, '%s %s %s' % (wheel_pick7, wheel_pick8, wheel_pick9))
        time.sleep(0.50)
        slot4 = await client.edit_message(slot3, '%s %s %s' % (wheel_pick10, wheel_pick11, wheel_pick12))
        time.sleep(0.50)
        slot5 = await client.edit_message(slot4, '%s %s %s' % (wheel_pick13, wheel_pick14, wheel_pick15))
        time.sleep(0.75)
        slot6 = await client.edit_message(slot5, '%s %s %s' % (wheel_pick16, wheel_pick17, wheel_pick18))
        time.sleep(0.75)
        slot7 = await client.edit_message(slot6, '%s %s %s' % (wheel_pick19, wheel_pick20, wheel_pick21))
        time.sleep(1)
        slot8 = await client.edit_message(slot7, '%s %s %s' % (wheel_pick22, wheel_pick23, wheel_pick24))
        time.sleep(1)
        slot9 = await client.edit_message(slot8, '%s %s %s' % (wheel_pick25, wheel_pick26, wheel_pick27))
        time.sleep(1)
        await client.edit_message(slot9, '%s %s %s' % (wheel_pick28, wheel_pick29, wheel_pick30))

client.run('clienttokenhere')

做一个函数就可以了!

def wheel():
    return SLOT_PATTERN[random.randint(0, len(SLOT_PATTERN) - 1)]

以及时间列表:

INTERVALS = [0.25, 0.25, 0.5, 0.75, 1, 1]

然后:

def wheels(n):
    return ' '.join(wheel() for _ in range(n))

if message.content.lower().startswith("!slots"):
    slot = await client.send_message(message.channel, wheels(3))

    for interval in INTERVALS:
        time.sleep(interval)
        slot = await client.edit_message(slot, wheels(3))