简体   繁体   English

IndentationError: 期望一个缩进块 - 不知道这是从哪里来的

[英]IndentationError: expected an indented block - Don't know where this comes from

I built this function and don't understand, why it doesn't work:我构建了这个 function 并且不明白,为什么它不起作用:

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

Ok, so I got it, thanks to KlausD.好的,我明白了,感谢 KlausD。 The code that actually doesn't work is the following, but I found the solution myself already.实际上不起作用的代码如下,但我自己已经找到了解决方案。 I had to dedent the comment in line 1.我不得不删除第 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    

import statements do not get indented python will throw an error usually those are at the top of your program so it would be like this: import 语句没有缩进 python 通常会抛出一个错误,这些错误通常位于程序的顶部,所以它会是这样的:

import datetime
def give_age_of(birthday):
    aging = birthday
    totoday = datetime.date.today()
    age_of = relativedelta(totoday,aging).years
    return age_of

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

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