【问题标题】:Random function doesn't work within discord bot随机功能在不和谐机器人中不起作用
【发布时间】:2018-07-21 16:19:17
【问题描述】:

我想得到一个包含 0-9 之间随机生成的数字的 lsit,我使用了:

for a in range(2**power):
    tree[levels-1].append(random.randint(0,9))

当在另一个程序中单独使用时,它会输出如下内容:[0, 3, 2, 4, 3, 9, 4, 8]。

但是,当我尝试在不和谐机器人的命令中的函数内执行此操作时:

第一个命令:

@client.command()
async def game():
    tree = await maketree(4)

命令中的函数:

def maketree(levels):
   for a in range(2**power):
       tree[levels-1].append(random.randint(0,9))

我得到错误: 'Command' 对象没有属性 'randint'

【问题讨论】:

  • 请提供minimal reproducible example。您很可能在程序中的某个时刻重新分配给random,如果没有minimal reproducible example,就不可能知道。
  • 不需要循环。使用具有如下范围的 random.choices():oneHundredRandomIntsBetween0and9inclusive = random.choices(range(10),k=100)
  • 是否应该将maketree 也设为异步以使其首先可以等待?

标签: python-3.x random python-asyncio discord discord.py


【解决方案1】:

您命名了一个命令random,它处理了您对random 模块的引用。您可以重命名该命令,以便仍然可以使用 !random 调用它

@bot.command(name="random", ...)
async def random_(...):
    ...

【讨论】:

    猜你喜欢
    • 2021-09-12
    • 2020-11-18
    • 2021-01-17
    • 2021-04-26
    • 2021-10-05
    • 2021-03-11
    • 2018-03-20
    • 1970-01-01
    • 2021-04-30
    相关资源
    最近更新 更多