def leap_year(year): # 判断平润年 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: return True else: return False def get_month_days(year, month): # 得到每个年份每月的天数 days = 31 if month == 2: if leap_year(year): days = 29 else: days = 28 elif month == 4 or month == 6 or month == 9 or month == 11: days = 30 return days def get_total_days(year, month): # 计算星期 total_days = 0 for y in range(1900, year): if leap_year(y): total_days += 366 else: total_days += 365 for m in range(1, month): total_days += get_month_days(year, m) return total_days if __name__ == "__main__": inp_year = int(input("输入年份:")) inp_month = int(input("请输入月:")) if inp_month not in range(1, 13): print("输入月份有误!") else: print("日\t一\t二\t三\t四\t五\t六") iCount = 0 week_bagin = (get_total_days(inp_year, inp_month) % 7) + 1 month_days = get_month_days(inp_year, inp_month)+1 for i in range(week_bagin): if week_bagin == 7: break else: print("\t", end="") iCount += 1 for i in range(1, month_days): iCount += 1 print(i, end="\t") if iCount % 7 == 0: print("") """ 简洁版: import calendar year = int(input("输入年份:")) month = int(input("输入月份:")) if month not in range(1, 13): print("输入月份有误") else: cal = calendar.month(year,month) print(cal) """ """ import calendar while True: print("输入0退出程序") year = int(input("输入年份:")) if year == 0: break month = int(input("输入月份:")) if month == 0: break if month in range(1, 13): cal = calendar.month(year, month) print(cal) else: print("输入月份有误!") """
日历【参考网上的一个模板】
最新推荐文章于 2019-05-02 22:12:11 发布