这几天课程学习了列表的操作,结合以前的函数知识,编写了一个能够判断天数的代码

源码如下

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)

 

相关文章:

  • 2021-12-18
  • 2021-05-05
  • 2021-08-13
  • 2021-09-26
  • 2022-01-09
  • 2022-12-23
  • 2021-07-07
  • 2021-12-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-04-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案