期计算年龄的代码
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()
¥
5.9
百度文库VIP限时优惠现在开通,立享6亿+VIP内容
立即获取
python输入出生日期和当前日期计算年龄_python根据出生日期计算年龄的代码
python输⼊出⽣⽇期和当前⽇期计算年龄_python根据出⽣⽇
期计算年龄的代码
python根据出⽣⽇期计算年龄的代码,运⾏后会提醒⽤户输出出⽣的年⽉⽇,然后输出年龄,可以改写为⼀个通⽤函数
from time import *
#a function to find your age
def age():
print "Enter Your Date of Birth"
第 1 页
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
cardno=raw_input("请输入身分证号:")year=cardno[6:10]
month=cardno[10:12]
day=cardno[12:14]
print "出生日:%s年%s月%s日"%(year,month,day)
1.定义一个get_constellation(month,date)函数,来获取出生日期。
2.
创建一个dates和constellations分别来储存对应的日和星座。
3.
用if语句判断输入的日数是否小于出生月份减一所对应的日数。
4.
如果是就返回月份减一所对应的星座,不是就返回出生月份所对应的星座。