如何计算 for 循环中的重复函数,Discord.py

How to count repeating functions in a for loop, Discord.py

我正在尝试创建一个 Discord 老虎机机器人,如果 2 个表情符号匹配,机器人会奖励用户一个奖品,如果所有 3 个都匹配,则将奖品乘以 3 倍。然而,为了做到这一点,我需要检查并查看我的函数是否重复任何值,我在网上寻找答案,我发现我认为我需要的封闭的东西是 .count if description_text.count(i): 然后如果两个或多个匹配的支出 x2 等,但由于我的函数是 int 而不是字符串,因此它不起作用。我也尝试对列表进行计数,但最后只返回 false,因为列表是唯一的。

我想知道你们是否有人知道我如何才能看到表情符号是否在重复,然后它们是否会相应地支付奖品。谢谢!

final = [
    "||:watermelon:||", #0
    "||:lemon:||", #1
    "||:tangerine:||", #2
    "||:apple:||", #3
    "||:pineapple:||", #4
]

description_text = ""

for i in range(3):
    description_text = f"{description_text} {random.choices(final, cum_weights=(0.4, 0.4, 0.2, 0.2, 0.2), k=1)[0]}"
    
embed = discord.Embed(title="Slot Machine", description= description_text, color=0x00d9ff)
embed.set_footer(text="Use !lottery to see how Prizes work!")
await ctx.send(embed=embed)

if description_text.count(i):
    await update_bank(ctx.author,2*amount)
    await ctx.send(f'You won :slight_smile: {ctx.author.mention}')
else:
    await update_bank(ctx.author,-1*amount)
    await ctx.send(f'You lose :slight_frown: {ctx.author.mention}')

错误消息: discord.ext.commands.errors.CommandInvokeError:命令引发异常:TypeError:必须是 str,而不是 int

最简单的解决办法就是重写咯。与其将每个插槽结果添加到字符串末尾来存储它们,不如分别生成它们:

slot1, slot2, slot3 = random.choices(final, cum_weights=(0.4, 0.4, 0.2, 0.2, 0.2), k=3)

然后,检查您的任何插槽是否相等应该很简单。您只需要从三个 slot 变量构建输出字符串:

description_text = f"{slot1}{slot2}{slot3}"