Python 练习实例4

[复制链接]
286|0
 楼主| cooldog123pp 发表于 2020-12-25 09:31 | 显示全部楼层 |阅读模式
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天:
程序源代码:
  1. #!/usr/bin/python3

  2. year = int(input('year:\n'))
  3. month = int(input('month:\n'))
  4. day = int(input('day:\n'))

  5. months = (0,31,59,90,120,151,181,212,243,273,304,334)
  6. if 0 < month <= 12:
  7.     sum = months[month - 1]
  8. else:
  9.     print ('data error')
  10. sum += day
  11. leap = 0
  12. if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
  13.     leap = 1
  14. if (leap == 1) and (month > 2):
  15.     sum += 1
  16. print ('it is the %dth day.' % sum)
结果:
  1. year:
  2. 2015
  3. month:
  4. 6
  5. day:
  6. 7
  7. it is the 158th day.


您需要登录后才可以回帖 登录 | 注册

本版积分规则

2304

主题

7626

帖子

31

粉丝
快速回复 在线客服 返回列表 返回顶部