繁体   English   中英

为什么这个 Python 代码在缩进完全正确时给我一个关于缩进的错误

[英]Why is this Python code giving me an error about indentation when the indentation is perfectly correct

我可能会在这个问题上获得大量的反对票,但我已经尝试在代码中再次缩进所有内容。 我仍然,出于某种原因得到一个IndendationError: expected an indentation block错误,这让我发疯。 我曾尝试在空​​格和制表符中再次缩进我的代码,但无济于事。 这是下面提供的代码:

async def menu(ctx):
    await ctx.message.delete()

    embed = discord.Embed(
            title = "Heartbeat Menu",
            description = "Select and enter a number.\n 1. Check Status of Heartbeat\n 2. Hearbeat Check"
        )
    sent = await ctx.send(embed=embed)
    
    def check(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel and msg.content in ["1", "2"]
    msg = await client.wait_for("message", check=check)

    if msg.content == "1":
        #code for number 1
    else:
        #code for number 2

错误出现在最后一行的 else 语句上。 确切的错误是:

else:   
^ IndentationError: expected an indented block  

我正在通过 discord 库(重写版本)编写Python机器人,以获得有关实际代码本身的一些上下文。

您收到错误是因为 else 语句中没有代码块。 你应该在 if 和 else 语句之后编写代码,或者如果你决定稍后编写代码,添加一个 pass 语句,一切都会正常工作。

if msg.content == "1":
   #code for number 1
   pass
else:
   #code for number 2
   pass

感谢@thomas 引起我的注意。 您在 else 语句上收到此错误,因为 if 语句后面没有代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM