首页 > 解决方案 > 如何更改正确答案的顺序或接受多个全局答案?

问题描述

我仍在研究我的问题机器人,现在我希望你能够提供多个correct_answers. 这是我的想法:每个用户都可以执行命令 3x 来给出答案。但是对于每个问题,我都希望有一个不同的解决方案被认为是正确的,所以并不总是仅仅A作为一个例子。我怎样才能让机器人认为以下答案按顺序正确:A, B, AA, B, C等等。

目前我有以下代码:

correct_answers = "A" #Only possibility

    @commands.command()
    async def answer(self, ctx, answer):
        self.answer.enabled = False
        global correct_answers
        if correct_answers != answer:
            await ctx.author.send(f "You guessed {answer} which is **wrong**. Good luck next time!")
            await ctx.message.delete()
            return
            [shortened]
            await ctx.message.delete()
        await ctx.author.send(f'The right answer was **{correct_answers}**, you guessed **correctly**!')
        await asyncio.sleep(10)
        self.answer.enabled = True

我想在哪个编辑之后决定哪个答案是正确的嵌入代码:

    @commands.command()
    async def trivia_c(self, ctx):

        e = discord.Embed(color=discord.Color.gold())
        e.title = "New question, new luck."
        e.description = "**When was Steve Jobs born?**"
        e.add_field(name="1️⃣", value="02/24/1955", inline=False)
        e.add_field(name="2️⃣", value="03/24/1955", inline=False)
        e.add_field(name="3️⃣", value="02/24/1965", inline=False)
        e.set_footer(text="You have x-x to answer this question.", icon_url=self.bot.user.avatar_url)
        e.timestamp = datetime.datetime.utcnow()
        question = await ctx.send(embed=e)

        await asyncio.sleep(10)

        e2 = discord.Embed(color=discord.Color.gold())
        e2.title = "New question, new luck."
        e2.description = "Test1"
        e2.add_field(name="1️⃣", value="02/24/1955")
        e2.add_field(name="2️⃣", value="03/24/1955")
        e2.add_field(name="3️⃣", value="02/24/1965")
        e2.set_footer(text="You have x-x to answer this question.")
        e2.timestamp = datetime.datetime.utcnow()
        await question.edit(embed=e2)

        await asyncio.sleep(10)

        e3 = discord.Embed(color=discord.Color.gold())
        e3.title = "Test2"
        e3.description = "When was Steve Jobs born?"
        e3.add_field(name="1️⃣", value="02/24/1955")
        e3.add_field(name="2️⃣", value="03/24/1955")
        e3.add_field(name="3️⃣", value="02/24/1965")
        e3.set_footer(text="You have x-x to answer this question.")
        e3.timestamp = datetime.datetime.utcnow()
        await question.edit(embed=e3)

        await asyncio.sleep(10)

我的方法是通过enumeration来实现,即, first, second, third = "A", "B", "A"或者correct_answers = ["A", "B", "B"]但在这两种情况下它都不起作用。我是否必须通过一个列表,我该怎么做?我还读到您可以通过index获得结果,但是对我来说它失败了。

总结: 第一次执行命令应该识别A为正确,然后第二次例如B,第三次例如C

标签: discorddiscord.py

解决方案


import random

@commands.command()
async def question(self, ctx):
    def check(m):
        return m.author == ctx.author and m.channel == ctx.message.channel
    
    question_one = "How many lives do cat's have?"
    answers_one = {"9":"a","1":"b","10":"c"}

    questions = {question_one:{"9":answers_one}, "Pineapple on pizza?":{"yes":{"yes":"a", 
        "no":"b"}}}

    # get a question
    question = random.choice(list(questions.keys()))
    data = questions.get(question)
    correct_answer = list(data.keys())[0]
    answers = list(list(data.values())[0].items())
    question_msg = ""
    answers_msg = ""

    numbers_list = []
    answers_list = []

    for answer, number in answers:
        numbers_list.append(number)
        answers_list.append(answer)

    while numbers_list != []:
        num = random.choice(numbers_list)
        ans = random.choice(answers_list)
        answers_msg += f"{num}. {ans}\n"
        answers_list.remove(ans)
        numbers_list.remove(num)

    question_msg = f"{question}\nPlease pick one of the following:\n{answers_msg}"
    question_sent = await ctx.send(question_msg)
    try:
        reply = await client.wait_for('message', check=check)
    except:
        return await question_sent.edit(content="You took too long, so the question is gone.")
    await reply.delete()
    if reply.content == correct_answer:
        await ctx.send("Well done!")
    else:
        await ctx.send("Nope. How can you get confused!")

这使用了wait_for,这意味着您可以共享数据而不需要多个命令。这支持使用多个问题。

在测试期间,我得到:

How many lives do cat's have?
Please pick one of the following:
c. 1
b. 10
a. 9
How many lives do cat's have?
Please pick one of the following:
a. 10
b. 9
c. 1

推荐阅读