IndentationError:预计会出现缩进块,我该怎么办?

时间:2018-06-12 15:08:44

标签: python

我目前正在用Python编写一些代码,每当我尝试运行该程序时,都会收到此错误。

这是代码;

 if month == time.strftime("%m") and day == time.strftime("%d"):
            continue
        elif hour == time.strftime("%H") and minute != time.strftime("%M"):
            channelD["just_sent"] = False
            with open("data.json", "w") as write_file:
                json.dump(data, write_file)
        elif hour == time.strftime("%H") and minute == time.strftime("%M"):
        if just_sent = channelD["just_sent"]:
            continue
            else:
                channel = bot.get_channel(i)
                await bot.send_message(channel, content=channelD["message"])
                print("24h message sent!")

2 个答案:

答案 0 :(得分:3)

也许你只需要像这样正确缩进:

if month == time.strftime("%m") and day == time.strftime("%d"):
    continue
elif hour == time.strftime("%H") and minute != time.strftime("%M"):
    channelD["just_sent"] = False
    with open("data.json", "w") as write_file:
        json.dump(data, write_file)
elif hour == time.strftime("%H") and minute == time.strftime("%M"):
    if just_sent == channelD["just_sent"]:  # notice == instead of =
        continue
    else:
        channel = bot.get_channel(i)
        await bot.send_message(channel, content=channelD["message"])
        print("24h message sent!")

我无法确定最后else:是否应位于其上方if的缩进级别或其上方elif的缩进级别。这取决于代码的语义。你应该检查一下。

Python解释缩进级别以确定块的开始和结束位置。在这方面它与许多其他语言不同。

答案 1 :(得分:2)

放手一搏

if month == time.strftime("%m") and day == time.strftime("%d"):
    continue
elif hour == time.strftime("%H") and minute != time.strftime("%M"):
    channelD["just_sent"] = False
    with open("data.json", "w") as write_file:
        json.dump(data, write_file)
elif hour == time.strftime("%H") and minute == time.strftime("%M"):
    if just_sent = channelD["just_sent"]:
        continue
    else:
        channel = bot.get_channel(i)
        await bot.send_message(channel, content=channelD["message"])
        print("24h message sent!")