用一个代码达到输入某人日期,则可以计算出他到今天为止存活过的天数
今天的日期按照 2020/2/16 来计算。
由于这个题目本身挺复杂,所以先实现一个更简单的题目:任意给出一个日期,给出这是这一年的第几天
- 这个代码是我第一个写的不是很聪明的办法,虽然也可以达到最终的目的,但是代码的篇幅很长,有大量不必要的部分。
year = int(input('请输入年份:'))
month = int(input('请输入月份:'))
date = int(input('请输入日期:'))
if year%4==0 and year%100!=0 or year%400==0:
ii = 29
else:
ii = 28
if month==1:
n = day
elif month==2:
n = 31+date
elif month==3:
n = 31+ii+date
elif month==4:
n = 31+ii+31+date
elif month==5:
n = 31+ii+31+30+date
elif month==6:
n = 31+ii+31+30+31+date
elif month==7:
n = 31+ii+31+30+31+30+date
elif month==8:
n = 31+ii+31+30+31+30+31+date
elif month==9:
n = 31+ii+31+30+31+30+31+31+date
elif month==10:
n = 31+ii+31+30+31+30+31+31+30+date
elif month==11:
n = 31+ii+31+30+31+30+31+31+30+31+date

使用Python编写代码,根据输入的出生日期计算到今天为止的存活天数。首先解决了一年中第几天的问题,通过判断闰年和建立月份天数列表来实现。接着扩展到计算生存天数,结果显示某人已活了10474天。
最低0.47元/天 解锁文章
6102

被折叠的 条评论
为什么被折叠?



