python根据出生日期计算年龄的代码,python出生日期计算,python根据出生日期


python根据出生日期计算年龄的代码,运行后会提醒用户输出出生的年月日,然后输出年龄,可以改写为一个通用函数

from time import *#a function to find your agedef age():    print "Enter Your Date of Birth"    d=input("Day:")    m=input("Month:")    y=input("Year:")    #get the current time in tuple format    a=gmtime()    #difference in day    dd=a[2]-d    #difference in month    dm=a[1]-m    #difference in year    dy=a[0]-y    #checks if difference in day is negative    if dd<0:        dd=dd+30        dm=dm-1        #checks if difference in month is negative when difference in day is also negative        if dm<0:            dm=dm+12            dy=dy-1    #checks if difference in month is negative when difference in day is positive    if dm<0:        dm=dm+12        dy=dy-1    print "Your current age is %s Years %s Months &amp; %s Days"%(dy,dm,dd)age()

评论关闭