【发布时间】: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