-1

我一直在尝试编写一个 Discord 机器人并制作一个 8ball,但它告诉我没有定义随机数。

@client.command(aliases=['8ball'])
async def _8ball(ctx, *, question):
        responses = [ "It is certain.",
                    "It is decidedly so.",
                    "Without a doubt.",
                    "Yes - definitely.",
                    "You may rely on it.",
                    "As I see it, yes.",
                    "Most likely.",
                    "Outlook good.",
                    "Yes.",
                    "Signs point to yes.",
                    "Reply hazy, try again.",
                    "Ask again later.",
                    "Better not tell you now.",
                    "Cannot predict now.",
                    "Concentrate and ask again.",
                    "Don't count on it.",
                    "My reply is no.",
                    "My sources say no.",
                    "Outlook not so good.",
                    "Very doubtful."]
        await ctx.send(f'Question: {question}\nAnswer: {random.choices(responses)}')

我得到了错误

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'random' is not defined

我的代码有问题吗?这是我第一次编码。

4

2 回答 2

1

在您的代码中的某处,您有一个名为random. 我看到它应该有一个名为choices. 您在 f 字符串的行中使用它:

await ctx.send(f'Question: {question}\nAnswer: {random.choices(responses)}')

确保您在此行之前定义了它。

或者,如果这是randomPython 核心中的标准,那么就像jasonharper建议的那样,确保您导入了它。

于 2020-02-09T18:13:00.400 回答
0

您可能需要import random 转到代码顶部import discord并向下输入,import random然后尝试参考:https ://docs.python.org/3/library/random.html

于 2020-02-11T02:52:20.830 回答