3

I'm a newbie in python programming and I have problem with my coding as the output doesn't display age correctly in days and seconds (as shown on 'output' below)

Output

Please enter your birthday
Year:1985
Month (1-12):5
Date:8
Your age is 10024 days, 0:00:00

Can anyone assist further why and how to fix this problem?

Code

print "Please enter your birthday "
bd_y=int(input("Year:"))
bd_m=int(input("Month (1-12):"))
bd_d=int(input("Date:"))

from datetime import date
now = date.today ()

age = date(int(bd_y), int(bd_m), int(bd_d))
print ("Your age is " + str(now-age))

Thanks in advance :)

Junuxx
  • 14,011
  • 5
  • 41
  • 71
Fra Arabella
  • 31
  • 1
  • 1
  • 2
  • At the beginning you're using the print statement (python 2.x) and on the end you're using the print function(python 3.x), you should fix that. – tehmisvh Oct 17 '12 at 15:46
  • 1
    @tehmisvh It's actually not a problem. It's ok to add the extra parenthesis to a `print` statement. I can only see a problem here if one tries to run this on Python 3, which wasn't the issue here. – jsalonen Oct 17 '12 at 15:48
  • 1
    What would be the output you expect? For me it seems the program works just alright. – jsalonen Oct 17 '12 at 15:49
  • What is your expected output? – ernie Oct 17 '12 at 15:51
  • 1
    To display the age with seconds precision you obviously need to know their exact birth time, not just a birthdate. Otherwise you assume everyone to be born at midnight. – georg Oct 17 '12 at 15:56
  • @jsalonen that is correct, but it's confusing to look at, especially because of him using input(), which are two different things in python 2.x and 3.x. – tehmisvh Oct 17 '12 at 15:57
  • Oh right. I get your point now! – jsalonen Oct 17 '12 at 15:59

4 Answers4

8

datetime.date only tracks year, month, and days. If you want the time as well you should use datetime.datetime:

>>> from datetime import datetime
>>> str(datetime.now() - datetime(1985, 5, 8))
'10024 days, 8:51:08.441714'

The result of the subtraction is a datetime.timedelta object, which has the attributes days, seconds, and microseconds for customizing output:

>>> delta = datetime.now() - datetime(1985, 5, 8)
>>> print "Your age is %d days and %d seconds" % (delta.days, delta.seconds)
Your age is 10024 days and 32125 seconds
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
4

You should use datetime instead of date.

Make sure you change the import too.

mariosangiorgio
  • 5,520
  • 4
  • 32
  • 46
2

The following works. You were converted to an int() twice, but that isn't what would break it.

I would suggest switching to datetime as suggested, but below is working with what you provided.

print "Please enter your birthday"
bd_y = input("Year: ")
bd_m = input("Month (1-12): ")
bd_d = input("Date: ")

from datetime import date
now = date.today()

birthdate = date(int(bd_y), int(bd_m), int(bd_d))

age =  now-birthdate

print "Your age is %s" % age
Lowerison
  • 21
  • 2
1
print("please enter your birthday")
bd_y=int(input("Year:"))
bd_m=int(input("Month(1-12):")) 
bd_d=int(input("Date:"))
from datetime import date
now = date.today ()

age = date(int(bd_y),int(bd_m), int(bd_d))
print ("your age is " +str(now-age))
Matthew
  • 11
  • 1