【问题标题】:Discord.py - SyntaxError f-string: empty expression not allowedDiscord.py - SyntaxError f-string:不允许空表达式
【发布时间】:2019-10-29 11:14:24
【问题描述】:

我收到 SyntaxError:f-string: empty expression not allowed 我不确定(完全)这是什么意思

我尝试移动代码并在网上查看,但没有得到不同的结果

代码是:

@client.command()
async def load(ctx, extension):
 client.load_extension(f'cogs.{extension}')

 await ctx.send(f"Loaded the {} module!".format(extension))

这是用于齿轮的,我确定其他一切都正确,但我不确定

如果有人知道该怎么做,请告诉我,谢谢

【问题讨论】:

  • 使用f'...'.format(..) 不要同时使用:f"Loaded the {} module!".format(extension))
  • 现在 Python 控制台甚至不想打开,它打开一瞬间然后关闭(我可以看到这是一个回溯错误,但仅此而已,它使用 IDLE 控制台打开,但我喜欢用 Python 的)
  • 用cmd提示符和终端打开并运行(运行后不会关闭)。这是一个不同的问题。

标签: python python-3.x discord.py-rewrite


【解决方案1】:

问题在于 f 字符串中的空 {}。 F 字符串需要大括号之间的变量名。 带有字符串的行应该是:

await ctx.send(f"Loaded the {extension} module!")

或者

await ctx.send("Loaded the {} module!".format(extension))

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    如果你想保持你的代码结构不变,你也可以这样做:

    await ctx.send(f"Loaded the {{}} module!".format(extension))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-08
      • 2020-11-27
      • 2013-08-12
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      相关资源
      最近更新 更多