问题描述
我正在尝试创建一个机器人,该机器人将根据我创建的列表中选择的“事件”吐出差异响应;将使用random.choice()选择它,但我不知道如何引用列表中的每个单独的东西?很抱歉,我不知道如何解释这个问题。
到目前为止,这是我的代码:
@client.command()
async def work(ctx):
await open_account(ctx.author)
users = await get_bank_data()
user = ctx.author
name = ctx.author.display_name
high = random.randrange(30,50)
low = random.randrange(0)
earnings = random.choice(str([f'{example 1}',f'{example 2}'])
if earnings == [0]: # i want to refer to the first 'thing' in the list here
await ctx.send(f"{earnings}\ncongratulations you have earned {low} coins")
if earnings == [1]: # and the second 'thing' here
await ctx.send(f"{earnings}\ncongratulations you have earned {top} coints")
解决方法
您可以创建列表并使用random.choice(YourList)
,因此以后可以参考YourList
possibilities = [example1,example2]
earnings = random.choice(possibilities)
if earnings == possibilities[0]:
await ctx.send(f"{earnings}\ncongratulations you have earned {low} coins")
elif earnings == possibilities[1]:
await ctx.send(f"{earnings}\ncongratulations you have earned {top} coints")