用python编写的关于“先输入一个年份,再输入一个月份,输出该月的天数”

终于自己写出了首个独立完成的python的程序,虽然也许编写的方法不是很完美,但至少功能有了。。。
题目是 “先输入一个年份,再输入一个月份,最终要输出该月的天数”。
在这个题目里要考虑到一个因素,那就是闰年的问题。
本人正好2月29日出生,独立诞生出这个程序也是有意义的嘛!

year = int((input(‘请输入年份’)))
moon = int((input(‘请输入月份’)))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0 and year % 3200 != 0) or year % 172800 == 0 :
years = int(‘0’)
else:
years = int(‘1’)

if moon is 2 and years is 0 :
print(‘该月是29天’)
if moon is 2 and years is 1:
print(‘该月是28天’)
else:
if moon in (1,3,5,7,8,10,12):
print(‘该月是31天’)
if moon in (4,6,9,11):
print(‘该月是30天’)

  • 10
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ```python year = int(input("请输入年份:")) month = int(input("请输入月份:")) if month in [1, 3, 5, 7, 8, 10, 12]: days = 31 elif month in [4, 6, 9, 11]: days = 30 else: if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: days = 29 else: days = 28 print("%d年%d共有%d天" % (year, month, days)) ``` 或者可以使用datetime模块: ```python import datetime year = int(input("请输入年份:")) month = int(input("请输入月份:")) days = (datetime.date(year, month+1, 1) - datetime.date(year, month, 1)).days print("%d年%d共有%d天" % (year, month, days)) ``` 需要注意的是,在使用datetime模块计算月份天数时,需要将输入月份加1,再计算和上一个的差值,才能得到当月天数。 ### 回答2: 编写程序,首从键盘上输入年份月份,然后根据输入年份月份计算这一年的这一共有多少天。 具体的步骤如下: 1. 从键盘上依次输入年份月份,并分别赋值给变量year和month。 2. 判断年份是否为闰年。如果满足以下两个条件之一,则为闰年:年份能被4整除但不能被100整除,或者能被400整除。 3. 根据月份判断这个天数。对于大(1、3、5、7、8、10、12),天数为31天;对于小(4、6、9、11),天数为30天;对于2,如果为闰年,则天数为29天,否则为28天。 4. 根据判断得到的天数输出结果。 下面是一个示例程序: ```python year = int(input("请输入年份:")) month = int(input("请输入月份:")) if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: leap_year = True else: leap_year = False if month in [1, 3, 5, 7, 8, 10, 12]: days = 31 elif month in [4, 6, 9, 11]: days = 30 elif month == 2: if leap_year: days = 29 else: days = 28 else: print("输入月份有误!") print("{}年{}共有{}天。".format(year, month, days)) ``` 运行程序后,根据输入年份月份,即可计算并输出这一年的这个共有多少天。 ### 回答3: 编写程序计算一个给定年份的一个给定月份天数可以按照以下步骤进行: 1. 首,从键盘上获取年份月份输入。 2. 使用条件语句判断所输入年份是否是闰年。 - 如果年份可以被4整除但不能被100整除,或者可以被400整除,则是闰年。这是因为闰年有366天(2有29天),而其他年份有365天(2有28天)。 3. 对于每个给定的月份,使用条件语句来确定它的天数。 - 对于1、3、5、7、8、10、12月份,它们都有31天。 - 对于4、6、9、11月份,它们都有30天。 - 对于2月份,如果是闰年,则有29天,否则有28天。 4. 输出计算得到的天数。 下面是一个用Python编写的示例程序: ```python # 从键盘上获取年份月份输入 year = int(input("请输入年份:")) month = int(input("请输入月份:")) # 判断年份是否是闰年 leap_year = False if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: leap_year = True # 判断每个月份天数 if month in [1, 3, 5, 7, 8, 10, 12]: days = 31 elif month in [4, 6, 9, 11]: days = 30 else: days = 29 if leap_year else 28 # 输出计算得到的天数 print(f"{year}年{month}共有{days}天") ``` 使用这个程序,你可以从键盘上输入任意的年份月份,它将计算并输出年份月份天数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值