从输入的日期(如2008年12月12日)判断当前日期是当年的第几天

本文介绍了一个程序,用于确定输入的特定日期(包括年、月、日)是所在年度的第几天。通过判断是否为闰年并计算月份之前的总天数,加上当月的日期,最终得出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从输入的日期(如2008年12月12日)判断当前日期是当年的第几天

 

程序如下

  1 r=0 #代表当年的第几天
  2 year=int(input('please enter the year:'))
  3 while True:
  4     month=int(input('please enter the month:'))
  5     if month<1 or month >12:
  6         print('the month does not exist. Please reenter it')
  7     else:
  8         break
  9 
 10 while True:
 11     day=int(input('please enter the day:'))
 12     if day<1 or day>30:
 13         print('the day does not exit,Please reenter it')
 14     else:
 15         break
 16 
 17 print('您选择的日期是:%s年%s月%s日'%(year,month,day))
 18 a=[31,28,31,30,31,30,31,31,30,31,30,31]
 19 
 20 
 21 if (year%4==0 and year%100!=0) or year%400==0:
 22     a[1]=29
 23 
 24 for i in range(0,12):
 25     if month>=i+2:
 26         r+=a[i]
 27     else: 
 28         month==0
 29 
 30 r+=day
 31 print(r)
 32 print('日期%s年%s月%s日是当年的第%s天'%(year,month,day,r))
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值