每个月有多少天python?

导读:今天首席CTO笔记来给各位分享关于每个月有多少天python的相关内容,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

在python中用if编写输入一个月份并计算有多少天

a = eval(input(\'请输入月份:\'))

while not(isinstance(a, int) and 0a13):

    a = eval(input(\'请输入正确的月份:\'))

da = [1,3,5,7,10,12]

xiao = [4,6,8,9,11]

if (a in da):

    print(a,\'月有31天\')

elif (a in xiao):

    print(a,\'月有30天\')

else:

    n=eval(input(\'请输入月所在年:\'))

    if  n%400==0 or (n%4==0 and n%100!=0):

        print(n,\'年为闰年\',a,\'月有29天\')

    else:

        print(n,\'年为平年\',a,\'月有28天\')

每个月有多少天python?  第1张

Python 判断指定月份的天数?

还要根据年份来的,闰年又不一样

year = int(input(\'请输入年份:\'))

month = int(input(\'请输入月份(1~12):\'))

if month == 2:

if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:

print(\'闰年29天\')

else:

print(\'平年28天\')

elif month in (4,6,9,11):

print(\'30天\')

else:

print(\'31天\')

python 获得一个月有多少天

在python的datetime模块中没有一个月有多少天的方法,但是可以使用calendar模块获得。

如下代码:

import calendar

monthRange = calendar.monthrange(2013,6)

print monthRange

输出:

(5, 30)

输出的是一个元组,第一个元素是上一个月的最后一天为星期几(0-6),星期天为0;第二个元素是这个月的天数。

python判断某一年的某个月有多少天

一三五七八十腊;

三十一天永不差;

四六九冬三十整;

平年二月二十八;

闰年二月把一加。

转化为代码就是:

y = int(input(\'请输入年份:\'))

m = int(input(\'请输入月份:\'))

if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:

    print(\"{}年{}月有31天\".format(y, m))

elif m == 4 or m == 6 or m == 9 or m == 11:

    print(\"{}年{}月有30天\".format(y, m))

elif m == 2:

    if y % 4 == 0:

        print(\"{}年{}月有29天\".format(y, m))

    else:

        print(\"{}年{}月有28天\".format(y, m))

else:

    print(\"输入有误\")

运行结果:

希望可以帮到你。

python 创建一个列表,依次存放每个月对应的天数,假设2月份的天数为28天,根据用户输入的月份?

l = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

while True:

a = eval(input(\'请输入月份(输入0退出):\'))

if a == 0:

exit()

try:

print(\'{}月共有{}天\'.format(a, l[a-1]))

except:

print(\'你的输入有误!\')

python任意输入一个月份(1—12),判断该月份有多少天(不考虑2月份29天的特殊情况)?

def month(n):

if n in [1,3,5,7,8,10,12]:

return 31

elif n in [4,6,9,11]:

return 30

elif n in [2]:

return 28

else:

return n, \" is not a month\"

结语:以上就是首席CTO笔记为大家整理的关于每个月有多少天python的全部内容了,感谢您花时间阅读本站内容,希望对您有所帮助,更多关于每个月有多少天python的相关内容别忘了在本站进行查找喔。

以上内容为新媒号(sinv.com.cn)为大家提供!新媒号,坚持更新大家所需的互联网后端知识。希望您喜欢!

版权申明:新媒号所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流,不声明或保证其内容的正确性,如发现本站有涉嫌抄袭侵权/违法违规的内容。请发送邮件至 k2#88.com(替换@) 举报,一经查实,本站将立刻删除。

(0)
上一篇 2023-09-23
下一篇 2023-09-23

相关推荐

发表回复

登录后才能评论