1

I am new to python, and have been doing a beginner's course on it.

I came across this piece of code during one of the lecture:

import datetime 
DOB=input("Enter the DOB:")
CurrentYear=datetime.datetime.now().year() 
CurrentAge=CurrentYear-int(DOB)
if(CurrentAge>=18):
    print("Your Age is {} and you are Adult".format(CurrentAge))
if(CurrentAge<18):
    print("Your Age is {} and you are not Adult".format(CurrentAge))
print("Condition has been verfied successfully")

This course that I am doing is from ~2018, and the instructor didn't have any error while compiling this code.

4
  • What input are you entering? Commented Aug 16, 2021 at 14:01
  • Yes, but i am still confused where exactly in this code is int being assinged twice Commented Aug 16, 2021 at 14:02
  • 1
    datetime.datetime.now().year is an int but you do datetime.datetime.now().year() - so you try to treat this int as a function Commented Aug 16, 2021 at 14:04
  • @SuneeshJacob 1998 , 2000, 2005. trying all ranging from the 90's Commented Aug 16, 2021 at 14:05

2 Answers 2

3

The issue is that the attribute year of datetime.datetime.now() is not a function, and so, it doesn't support year(). That's why it says that it is not callable. datetime.datetime.now().year is in int format, and since it is not a function, it is not callable (like this:.()).

Change CurrentYear=datetime.datetime.now().year() to CurrentYear=datetime.datetime.now().year.

The new code would be like this:

import datetime 
DOB=input("Enter the DOB:")
CurrentYear=datetime.datetime.now().year
CurrentAge=CurrentYear-int(DOB)
if(CurrentAge>=18):
    print("Your Age is {} and you are Adult".format(CurrentAge))
if(CurrentAge<18):
    print("Your Age is {} and you are not Adult".format(CurrentAge))
print("Condition has been verfied successfully")
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I finally got the output.
can you explain what was the issue with the previous code?
The issue with the previous code is that the attribute year of datetime.datetime.now() is not a function, and so, it doesn't support year(). That's why it says that it is not callable. datetime.datetime.now().year is in int format, and since it is not a function, it is not callable (like this:.()). I will add this info in the answer.
"year" is an attribute of datetime class not a function. Hence, not callable. To access the attributes you don't have to use "()"
2

The issue is data types (hence TypeError). A data type may be a string, integer, or even a function (a function is something that is called, such as print(): notice the brackets).

You cannot put the brackets after an integer. That suggests you are trying to run it as a function.

The variable: datetime.datetime.now().year is an integer. You cannot run a function from an integer.

The correction would be from datetime.datetime.now().year()datetime.datetime.now().year

The final code looks like this:

import datetime 
DOB=input("Enter the DOB:")
CurrentYear=datetime.datetime.now().year 
CurrentAge=CurrentYear-int(DOB)
if(CurrentAge>=18):
    print("Your Age is {} and you are Adult".format(CurrentAge))
if(CurrentAge<18):
    print("Your Age is {} and you are not Adult".format(CurrentAge))
print("Condition has been verfied successfully")

I have tested this thoroughly. Good luck with the rest of your Python course.

2 Comments

thank you for the explanation. i wasn't able to notice it while coding simultaneously during the course and thus ended up creating a function.
You are very welcome, @lonewolf303. Data types can be confusing to start with. Geed luck with your Python journey! 👋

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.