python打印日历
#未优化的代码
1 #输出日历
def print_calendar(year,month,date = 1):
month_dict = {'':'January','':'February','':'March','':'April','':'May','':'June','':'July',
'':'August','':'September','':'October','':'November','':'December'} #数字月份转换为字符串,并判断月份和号数是否合法
if month in range(1,13) and date in range(1,31):
month_str = str(month)
if month_str in month_dict:
month_str = month_dict[month_str]
else:
print('月份或号数输入不合法')
return -1 #头部
print('%15s%8d'%(month_str,year))
print('-'*33)
print('Sun Mon Tue Wed Thu Fri Sat') #得到每月1号是星期几
first_day = get_start_day(year,month,1)
#得到此月有多少天
month_num = days_of_month(year,month) each_day = 0
#主体
for index in range(1,43): if index < first_day + 1:
print(' '*5,end = '')
else:
if (index - 1) % 7 == 0:
print('')
each_day += 1
if each_day > month_num:
return False
if each_day < 10:
if each_day == date:
print('%-5s'%('--'),end = '')
else:
print(' %-4d'%(each_day),end = '')
else:
if each_day == date:
print('%-5s'%('--'),end = '')
else:
print('%-5d'%(each_day),end = '') #输入一个年月日,判断是星期几
#需要一个比较标准:2010-1-1是星期五
#计算当前距离标准过了多少天(total_days % 7 + 5 -1)%7
#先遍历年份,是闰年+366,不是+365
#再遍历月份,31,30,29,28
def get_start_day(year,month,date):
total_days = 0
#遍历年份
for one_year in range(2010,year):
if is_leap_year(one_year):
total_days += 366
else:
total_days += 365
#print(total_days)
#遍历月份
for one_month in range(1,month):
total_days += days_of_month(year,one_month)
#print(total_days)
#加上当月号数,则求得总共过了多少天
total_days += date #求输入的年月日是星期几
day = (total_days % 7 + 5 - 1) % 7 #print(total_days)
#print(day)
return day #输入一个年份和月份,输出这月有多少天
#1,3,5,7,8,10,12--------31天
#4,6,9,11 --------------30天
#如果是闰年2------------29天
#不是闰年 2-------------28天
def days_of_month(year,month):
days = 0
if month in (1,3,5,7,8,10,12):
days = 31
elif month in (4,6,9,11):
days = 30
elif is_leap_year(year):
days = 29
else:
days = 28
return days def is_leap_year(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True
return False def main():
print('*'*33)
year = int(input('请输入年份:'))
month = int(input('请输入月份:'))
date = int(input('请输入号数:'))
print('*'*33)
#某年某月有多少天
#days = days_of_month(year,month)
#print('{}年{}月有{}天'.format(year,month,days))
#某年某月某日是星期几
#day = get_start_day(year,month,date)
#print('{}年{}月{}日是星期{}'.format(year,month,date,day))
#打印日历
print_calendar(year,month,date) #执行
main()

python打印日历的更多相关文章
- python 打印日历
import calendar as c'''x = c.monthcalendar(2017,11) 使用这个结果打印出日历 s = 1while s <= 7: print('周%d '%( ...
- Python学习实践-----打印日历
使用python语言实现在控制台打印日历 输入年.月.日 输出对应日历,指定的日数输出为'--' 程序没有做严格的输入验证,故输入整数即可. 以下为没有优化的源码: print_calendar.py ...
- Python实战练习——打印日历教程
很长一段时间没有接触过C语言了,想来做这一行当已经有三两年了. 今天突然想起来以前用C语言在VC6上写代码的日子,想了想以前的一些实战练习. 所以今天打算用Python来写一个C语言以前练习的题目-日 ...
- Python打印格式化与字符串
关于Python打印格式化与字符串,比较全面的总结,希望对大家有帮助~ # -*- coding: cp936 -*- ''' 打印格式 ''' print "a" print & ...
- python打印表格式数据,留出正确的空格和段落星号或注释
python打印表格式数据,留出正确的空格,格式化打出 代码如下: def printPicnic(itemsDict,leftWidth,rightWidth): print('PICNIC ITE ...
- python打印万年历
1.输入年份,输入月份 2.格式化输出本月的日历 3.思路 输入年,月,打印对应年月的日历.1,首先1970年是Unix系统诞生的时间,1970年成为Unix的元年,1970年1月1号是星期四,现在大 ...
- Oracle打印日历功能
Oracle用SQL打印日历 1.1 打印当月日历 , D, NULL)) SUN, , D, NULL)) MON, , D, NULL)) TUE, , D, NULL)) WED, , D, ...
- 使用java 打印日历
package hangshu; /* * 打印从1900年到2.year年的日历 */ import java.util.Scanner; public class Calender { publi ...
- python 打印 emoji
python 打印 emoji 如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/10486506.html 一.Unicode字符集: ...
随机推荐
- [CSS3] Use Sticky Positioning for Section Headers
We can take advantage of sticky positioning to keep a section header at the top of the page while th ...
- matlab中怎样加入凝视
1)方法一 选中你要加凝视的内容,然后选择工具菜单"text|comment"就能够了,假设要把凝视变为语句,相同选中要转变的语句,然后用鼠标选择"text|uncomm ...
- Axure RP一个专业的高速原型设计工具
Axure RP是一个专业的高速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司.RP则是Rapid Prototyping(高速原型)的缩写. Axure简要介绍 Axur ...
- hadoop常见操作命令
1.查看指定文件夹下内容 hadoop dfs –ls [文件文件夹] eg: hadoop dfs –ls /user/wangkai.pt 2.打开某个已存在文件 hadoop dfs –cat ...
- Qt graphic item日记
今天在用用graphic view 加入graphic item的时候要引入一个context menu,自然就要对context menu上的action进行slot处理.可是graphic ite ...
- IE訪问Oracle EBS打不开Form的问题
IE訪问Oracle EBS打不开Form的问题 例如以下图. 最后我才知道真正的原因.原来是兼容性视图的问题. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5 ...
- 使用 Swift 3.0 操控日期
作者:Joe,原文链接,原文日期:2016-09-20译者:Cwift:校对:walkingway:定稿:CMB 当你在想要 大规模重命名 时,一个附带的挑战就是要确保所有相关的文档都必须同步更新.比 ...
- PCB MVC启动顺序与各层之间数据传递对象关系
准备着手基于MVC模式写一套Web端流程指示查看,先着手开发WebAPI打通数据接口,后续可扩展手机端 这里将MVC基本关系整理如下: 一.MVC启动顺序 二.MVC各层之间数据传递对象关系
- Appium + python - long_press定位操作实例
from appium.webdriver.common.touch_action import TouchActionfrom appium import webdriverimport timei ...
- selenium3 + python - alert_is_present定位
from selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium ...