简体   繁体   中英

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:

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. 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.

    # 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 datetime
def give_age_of(birthday):
    aging = birthday
    totoday = datetime.date.today()
    age_of = relativedelta(totoday,aging).years
    return age_of

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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