【问题标题】:IndentationError: expected an indented block - Don't know where this comes fromIndentationError: 期望一个缩进块 - 不知道这是从哪里来的
【发布时间】:2021-11-22 21:00:11
【问题描述】:

我构建了这个功能,不明白,为什么它不起作用:

def give_age_of(birthday):

    import datetime
    from dateutil.relativedelta import relativedelta

    aging = birthday
    totoday = datetime.date.today()
    age_of = relativedelta(totoday,aging).years

    return age_of
 File "<ipython-input-258-df8b9618ac1f>", line 5
    import datetime
    ^
IndentationError: expected an indented block

【问题讨论】:

  • 您发布的代码中没有缩进错误。确保您没有混合制表符和空格。
  • 你的第5行实际上是第3行有点可疑。
  • 这能回答你的问题吗? I'm getting an IndentationError. How do I fix it?
  • 顺便说一句,欢迎来到 Stack Overflow!如果您需要提示,请查看How to Ask。
  • 如果你有一个带有行号的回溯,你应该确保它们匹配。

标签: python syntax


【解决方案1】:

好的,我明白了,感谢 KlausD。实际上不起作用的代码如下,但我自己已经找到了解决方案。我不得不删除第 1 行的注释。

    # blablabla

def give_age_of(birthday):

    import datetime 
    from dateutil.relativedelta import relativedelta

    aging = birthday
    totoday = datetime.date.today()
    age_of = relativedelta(totoday,aging).years

    return age_of    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多