gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:55:53 25 4
gpt4 key购买 nike

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

基本上它显示了 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))

关于python - 编写递归随机变量的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44223582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com