用Python实现计算出生天数

首先输入出生年月日与现在的年月日

 #提取身份证中的年月日,输入现在年月日
a = input('请输入身份证:');
b = a[6:10]
c = a[10:12]
d = a[12:14]
print('您出生于%s年%s月%s日'%(b,c,d))
year = input('请输入现在是哪一年:')
month = input('请输入现在是哪一月:')
date = input('请输入现在是哪一日:')

判断从出生到现在有几个闰年,先假定所有天数都是365天,最后再加上闰年年数 

t = 0 
for n in range(int(b),int(year)):
    if n%4 !=0:
        continue
    t = t+1

计算今天与生日相差几天,加在总天数里

other_day = 0
now_day = 0
month_day=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year_day = (int(year)-int(b))*365 #估算的总天数
#生日天数
for g in range(int(c)-1):
    other_day = other_day+month_day[g]
other_day = other_day+int(d)
#现在天数
for f in range(int(month)-1):
    now_day = now_day + month_day[f]
now_day = now_day + int(date)

last_day = now_day - other_day
all_day = last_day+year_day+ t  #得到相差天数
print("你已出生%d天”%(all_day))

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值