python3练习题:输入某年某月,判断这一天是这一年的第几天?

# 闰年规则:四年一闰,百年不闰,四百年再闰
# 定义函数,判断是否为闰年
def leapyear(year):
    return (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0))


# 定义一个列表,储存每月天数
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# 储存月份的天数
total = 0
while True:
    # 由用户输入年、月、日
    year = int(input('请输入四位数年份:'))
    month = int(input('请输入月份:'))
    day = int(input('请输入日期:'))
    # 限制月份、日期范围
    if 0 < month <= 12 and 0 < day <= 31:
        print('您输入的月份和日期正确')
        if leapyear(year):
            # 若是闰年,二月改为29天
            days[1] = 29
        # 遍历列表,对应月份的天数传给tatal存储
        for i in range(month - 1):
            total += days[i]
        break
    else:
        print('您输入的月份或日期不正确,请重新输入')

print('这是{}年的第{}天'.format(year, total + day))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值