用python计算某年某月某日是该年的第几天

没有用日期函数,综合字典的功能,利用for循环计算。源码自刘卫国的python程序设计教程,有修改。测试成功代码如下:

year=int(input('请输入年份:'))
month=int(input('请输入月份:'))
day=int(input('请输入日期:'))
days=0
dic={'1':31,'2':28,'3':31,'4':30,'5':31,'6':30,'7':31,'8':31,'9':30,'10':31,'11':30,'12':31}

if year%4 == 0 and year%100 ==0 or year%400 == 0:
    dic['2']=29
if int(month)>1:
    for obj in dic.keys():
        if month==int(obj):
            for i in range(1,int(obj)):
                days+=dic[str(i)]
    days+=day
else:
    days=day
print(f'{year}年{month}月{day}日是该年的第{days}天。')

你可能感兴趣的:(python,python)