Python 利用函数、列表来实现计算天数
这几天课程学习了列表的操作,结合以前的函数知识,编写了一个能够判断天数的代码
源码如下
def is_year(year):
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0 #判断年份是否为闰年,是闰年则返回1,
def calculate(year,month,day):
is_month=[[31,28,31,30,31,30,31,31,30,31,30,31],[31,29,31,30,31,30,31,31,30,31,30,31]]
a=is_month[is_year(year)] #平年取第一个列表,闰年取第二个
b=0
for i in range(month-1): #把输入月份的前几个月份加起来
b+=a[i-1]
c=b+day #总天数
print("这是%d年的第%d天"%(year,c))
year=eval(input("请输入年份:"))
month=eval(input("请输入月份:"))
day=eval(input("请输入日期:"))
calculate(year,month,day) 相关推荐
ztyzly00 2019-08-28
manifold 2019-01-01
AI世代 2019-06-19
CSDN社区管理员 2018-06-16
MingDaw 2017-04-24
HackAge 2016-07-22
luochaotj 2016-01-25
talang 2017-05-23
鲁鲁酱 2017-05-10
CoffeyLiu 2013-12-27
lundongcai 2014-02-09
MySQL源氏boy 2019-04-13
PHP100 2019-03-28
PHP100 2019-03-28
PHP100 2019-03-28
PHP100 2019-03-28
PHP100 2019-03-27
扑克投资家 2017-12-30
软件设计 2017-07-12