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

2015-06-09 0 219
python根据出生日期计算年龄的代码

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

from time import *
#a function to find your age
def 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 & %s Days"%(dy,dm,dd)
     
age()

遇见资源网 python python根据出生日期计算年龄的代码 http://www.ox520.com/15454.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务