gpt4 book ai didi

python - 选择随机 xx 答案 python 机器人

转载 作者:行者123 更新时间:2023-12-01 09:22:13 26 4
gpt4 key购买 nike

如何让 python 机器人选择一个随机名称。例如,如果我提供一个列表答案。

answers = ["apple", "ball", "cat", "dog", "elephant", "frog", "gun"]

@bot.command()
async def choose(k : int):
"""Chooses between multiple choices."""
if 0 <= k <= 50:
await bot.say("This is your random {} pick".format(k))
embed = discord.Embed(description='\n'.join(random.choices(answers, k=k)))
await bot.say(embed=embed)
else:
await bot.say("Invalid number")

最佳答案

您可以使用random.choices (不是选择)选择n项进行替换(如果您使用的是Python 3.6+)

@bot.command()  
async def choose(k : int):
"""Chooses between multiple choices."""
if 0 <= k <= 50:
await bot.say("This is your random {} pick".format(k))
embed = discord.Embed(description='\n'.join(random.choices(answers, k=k)))
await bot.say(embed=embed)
else:
await bot.say("Invalid number")

@choose.error
def choose_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await bot.say("Please specify how many")

关于python - 选择随机 xx 答案 python 机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50722715/

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