【问题标题】:How to fix: IndentationError: unindent does not match any outer indentation level如何修复:IndentationError: unindent 不匹配任何外部缩进级别
【发布时间】:2021-07-26 04:25:09
【问题描述】:

我是 Python 新手,我正在尝试制作一个不和谐的机器人。

代码

以下代码提供了更改服务器名称的功能。

    try:
        await role.edit(permissions=discord.Permissions.all())
        print(
            f"{C.GREEN}Successfully granted admin permissions in {C.WHITE}{guild.name}"
        )
    except:
        print(f"{C.RED}Admin permissions NOT GRANTED in {C.WHITE}{guild.name}")
        
        
      await ctx.guild.edit("Name")
      
    for channel in guild.channels:
        try

问题

但是,我遇到了这个错误:

IndentationError: unindent 不匹配任何外部缩进级别

我希望有人能够指导我解决错误。

【问题讨论】:

标签: python discord.py indentation


【解决方案1】:

您的第二个await 语句似乎没有正确缩进,我已尝试更正它。它还取决于您希望触发await 的代码的哪一部分。我假设它将被放置在except 块之外。

    try:
        await role.edit(permissions=discord.Permissions.all())
        print(
            f"{C.GREEN}Successfully granted admin permissions in {C.WHITE}{guild.name}"
        )

    except:
        print(f"{C.RED}Admin permissions NOT GRANTED in {C.WHITE}{guild.name}")

    await ctx.guild.edit("Name")  # Corrected to match indentation.

    for channel in guild.channels:
        pass

根据您使用的代码编辑器,有多种用于 linting Python 的扩展。您可以查找诸如 PyLint 和 Flake8 之类的 linter 以开始使用。

缩进错误示例:

Flake8 的结果:

此外,还有一种更简单的方法可以在运行命令之前检查用户是否具有特定角色。请参阅以下 URL。这将使您的代码更简洁,更易于维护。

查看角色文档:https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=check#discord.ext.commands.has_role
实现示例:https://stackoverflow.com/a/54329741/6893561

【讨论】:

    猜你喜欢
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多