python万年日历_python 打印万年历

题目:打印万年历

已知条件

闰年条件:能被4整除且不能被100整除,或者能被400整除

1900年1月1日 是周一

解题思路

判断闰年;

判断当月有多少天;

这个月的1号是从周几开始的;

格式化打印日历。

解题代码

#判断年份是否为闰年

def is_leap_year(year):

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

return True

else:

return False

#判断月份有多少天

def get_month_day(year,month):

days=31

if month in [4,6,9,11]:

days=30

elif month == 2:

if is_leap_year(year):

days=29

else:

days=28

return days

#求输入年份和月份日期总天数

def get_days(year,month):

totaldays=0

for i in range(1900,year):

if is_leap_year(i):

totaldays+=366

else:

totaldays+=365

for i in range(1,month):

totaldays+=get_month_day(year,i)

return totaldays

#主程序

if __name__ == '__main__':

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

month = input('请输入月份:')

try:

year = int(year)

month = int(month)

if month < 1 or month > 12:

print('月份输入错误,请重新输入')

continue

except:

print('年份或月份输入错误,请重新输入')

continue

break

print('日\t一\t二\t三\t四\t五\t六')

count = 0

for i in range((get_days(year,month)%7)+1):

print('\t',end='')

count+=1

for i in range(1,get_month_day(year,month)+1):

print(i,end='')

print('\t',end='')

count+=1

if count%7 ==0:

print('/n')

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值