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

本文介绍了一个简单的Python程序,用于计算用户输入的特定日期是一年中的第几天。通过判断是否为闰年并考虑不同月份的天数,该程序能准确给出答案。

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

from pip._vendor.distlib.compat import raw_input

year = int(raw_input("please enter the year :"))
month = int(raw_input("please enter the month:"))
day = int(raw_input(" please enter the day:"))

#每月第一天,默认2月为28天
months = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)

if 0 <= month <= 12:
    sum = months[month - 1]
else:
    print("data error !")

#获取当前总天数
sum += day

# 判断是否为闰年
leap = False

if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
    leap = True
    if leap is True and month == 2:
        sum += 1

print("您输入的日期为"+str(year)+"年的"+str(sum)+"天!")
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值