……

题目:输入某年某月某日,判断这一天是这一年的第几天?

程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天:

year=int(input("请输入年份:\n"))
month=int(input("请输入月份:\n"))
day=int(input("请输入天数:\n"))
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 <month <=12:
    sum=months[month-1]
else:
    print("输入错误!!")
sum +=day
leap =0
if (year%400==0) or ((year%4==0) and (year%100!=0)):
    leap=1
if (leap==1) and (month>2):
    sum+=1
print(year,month,day,'是%dth 天.' % sum)

结果是:

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=58506
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])
Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.12.0
Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32
runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')
请输入年份:
2020
请输入月份:
10
请输入天数:
20
2020 10 20 是294th 天.

 

 posted on 2020-10-20 14:41  大码王  阅读(189)  评论(0编辑  收藏  举报
复制代码