【问题标题】:The Number Of Points Isnt Changing... Please Fix点数没有变化...请修复
【发布时间】:2022-01-21 21:05:15
【问题描述】:

我试图提出一个快速射击类型的问题,基本上“j”是积分的变量,我希望它每 0.5 秒更改/减少(并检查消息)......问题是无论如何迟到我尝试我得到 1000 分。我也是新人。 (顺便说一句对不起英语不好)

代码:

    def check(m):
        return m.author == message.author and m.channel
    mn = randint(5, 15)
    mn1 = randint(5, 10)
    mz = mn * mn1
    membed = discord.Embed(
        title="Here's The Question", 
        description=str(mn) + " * " + str(mn1) + ''' Type Your Answer Below.. ''',
        url=None,
        color=discord.Color.blue())
    mzz = await message.send(embed=membed)
    j = 1100
    for i in range(0, 5):
        sleep(0.5)
        j = j - 100
        gt = await bot.wait_for('message', check=check)
        if int(gt.content) == int(mz):
            await message.send(f'Its Right.. You Got **{j}** Points')
        else:
            await message.send(f'Its Wrong.. The Answer Is **{j}**')

请帮忙...

【问题讨论】:

  • 这不足以帮助调试。 message 是什么?当您说“点数没有变化”时,您是否看到变量 j 没有被修改,或者其他什么?
  • 我希望 j 每 0.5 秒减少一次,并且我希望它每 0.5 秒检查一次消息...问题是,无论我输入多晚,我都会得到 1000 分

标签: python discord discord.py message


【解决方案1】:

问题是因为您误解了await 的工作原理。

await foo() 使您的程序休眠直到 foo() 返回一个值。

当您输入await bot.wait_for() 时,您是在说:

  1. 休眠直到bot.wait_for()返回值
  2. bot.wait_for() 仅在收到消息时返回一个值。

您的程序的完整流程如下:

  1. 您进入第一个循环i = 0j 设置为 1000。
  2. 您会一直睡觉,直到收到消息。因为您可以在未来的任何时间(例如,十秒或十分钟或十五小时等)收到一条消息,所以您永远不会进入第二个循环i = 1。基本上,您的程序此时会被冻结,直到有人向您发送消息。

这里的解决方案是让你的机器人超时等待消息,这样它就可以继续到下一个值i。超时告诉您的机器人在继续您的程序之前仅休眠指定的时间。方便的是,wait_for 命令提供了一个timeout 参数:

j = 1100
for i in range(0, 5):
    j = j - 100
    try: # A try/except block is needed because this throws an error if bot times out
        gt = await bot.wait_for('message', check=check, timeout=0.5)
        if int(gt.content) == int(mz):
            await message.send(f'Its Right.. You Got **{j}** Points')
            break
        else:
            await message.send(f'Its Wrong.. The Answer Is **{j}**')
    except:
        continue
else:
    await message.send("Whoops, you're out of time! You got zero points.") 

【讨论】:

  • 即使我写了答案,它也会发送超时消息
  • 已更新。我使用了一个鲜为人知的 Python 功能,称为 for/else 循环。
  • 即使我写了正确的答案,它仍然显示超时消息,是否有错误??
  • 没关系我忘记了 break 命令...抱歉
  • 如果此答案对您有用,请点赞并标记为正确。
猜你喜欢
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多