美文网首页自学编程Python日更成长营
自学Python:某年某月是这一年的第几天

自学Python:某年某月是这一年的第几天

作者: 小强聊成长 | 来源:发表于2022-04-24 14:36 被阅读0次

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

比如输入2021年12月10日,如何能知道这是2021年的第几天了。

现在用程序来实现随意输入日期,然后给出结果。

下面直接上代码:

########################

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('本年的第 %d 天。' % sum)

########################

执行结果如下:

请输入年:

2021

请输入月:

12

请输入日:

10

本年的第 344 天。

________________END______________

相关文章

网友评论

    本文标题:自学Python:某年某月是这一年的第几天

    本文链接:https://www.haomeiwen.com/subject/fcgjdrtx.html