今天初学Python写了一个用于计算指定年指定月日历的脚本

我的Python版本:Python 3.4.2

输入:脚本名 年(4位数字,1900-2100) 月(1-2位数字,1-12)

输出:打印的指定年月日历信息

Calendar.py

import os

import sys

# check if the number of input is legal

if len(sys.argv) != 3:

print('Invalid input! Example: 2014 12')

os.system('pause') # "press any key to continue..."

os._exit(0) # terminate this script

# check if input(year) is legal

print("Year: %s" % sys.argv[1])

if not str(sys.argv[1]).isdigit():

print('Invalid input! Year must be a positive integer')

os.system('pause')

os._exit(0)

elif int(sys.argv[1]) < 1900 or int(sys.argv[1]) > 2100:

print('Invalid input! Year must bigger than 1900 and smaller than 2100')

os.system('pause')

os._exit(0)

# check if input(month) is legal

print("Month: %s" % sys.argv[2])

if not str(sys.argv[2]).isdigit():

print('Invalid input! Month must be a positive integer')

os.system('pause')

os._exit(0)

elif int(sys.argv[2]) < 1 or int(sys.argv[2]) > 12:

print('Invalid input! Year must bigger than 1 and smaller than 12')

os.system('pause')

os._exit(0)

# check: is a leap year or not

# param @year: the year input

# return: leap: True; not leap: False

def IsLeapYear(year):

if year % 4 != 0:

return False

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

return False

return True

cur_year = sys.argv[1] # the year input

cur_month = sys.argv[2] # the month input

# counter: the first day in cur_year, cur_month, it indicates the day of week

counter = 0

for i in range(1900, int(cur_year)):

if IsLeapYear(i):

counter += 366

else:

counter += 365

days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

days_in_month_leap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

for i in range(1, int(cur_month)):

if not IsLeapYear(int(cur_year)):

counter += days_in_month[i - 1]

else:

counter += days_in_month_leap[i - 1]

# first_day_in_cur_month: what day is the first day in cur_month

first_day_in_cur_month = counter % 7

# name of each month

month_name = ['January', 'February', 'March', 'April', 'May', 'June', 'July',

'August', 'September', 'October', 'November', 'December']

# char numbers of each line

calendar_width = 45

# print title

print()

print('=' * calendar_width)

space_num = (calendar_width - len(month_name[int(cur_month) - 1])) // 2

sys.stdout.write(" "* space_num)

sys.stdout.write(month_name[int(cur_month) - 1])

sys.stdout.write("\n")

print('=' * calendar_width)

print(" MON TUE WED THU FRI SAT SUN")

print('=' * calendar_width)

# establish a calendar

# calendar = [[0] * 7] * 6 # can not do like this! change a number then change a column

calendar = [[0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0]]

days_count = 0

if not IsLeapYear(int(cur_year)):

days_count = days_in_month[int(cur_month) - 1]

else:

days_count = days_in_month_leap[int(cur_month) - 1]

for i in range(0, days_count):

x = (first_day_in_cur_month + i) // 7

y = (first_day_in_cur_month + i) % 7

calendar[x][y] = i + 1

# print calendar

for i in range(0, 6):

if(i != 0 and calendar[i][0] == 0): # no more days to output then break

break;

sys.stdout.write(" " * 3)

for j in range(0, 7):

str_date = str(calendar[i][j])

sys.stdout.write(" ")

if str_date == "0":

sys.stdout.write(" ")

elif len(str_date) == 1:

sys.stdout.write(str_date)

sys.stdout.write(" ")

else:

sys.stdout.write(str_date)

sys.stdout.write(" " * 3)

sys.stdout.write("\n")

# print the end line

print('=' * calendar_width)

print()

# os.system('pause')

补充说明:使用 Visual Studio 上的 Python 插件时,调试时要设置命令行输入参数,需要进行如下两步

1)项目→Calendar属性(Calendar为项目名)

2)在属性界面的Debug选项卡中,设置“Script Arguments”,这个程序的输入为“2014 10”

优化:(2014年12月31日)

这3个优化的地方都需要引用calendar,设变量year存储年,变量month存储月

1)遍历一个月的所有天,在本文的代码中,用range(0, days_count),

可以用calendar.monthrange(year, month)代替

2)找出一个月的第一天是星期几,之前用first_day_in_cur_month保存,

可以用calendar.weekday(year, month, 1)代替

calendar.weekday函数中,星期一则返回0,星期二则返回1,以此类推,星期日返回6

3)日历矩阵可以直接用calendar.monthcalendar(year, month)得出

END

python怎么调出某年某月日历_Python 写的计算指定年指定月日历的脚本相关推荐

  1. python代码命令行tab补齐_Python在命令行下Tab键自动补全脚本

    摘要 由于写shell命令习惯了Tab补全,其实我个人认为这不是应该有的一个恶习,基础开始我们不应该想着Tab去自动补全,我们应该趁这时候来多多记忆基础的东西.唉,恶习难改...... 老实说我也是网 ...

  2. python算闰年和平年的天数_Python自定义函数计算给定日期是该年第几天的方法示例...

    本文实例讲述了Python自定义函数计算给定日期是该年第几天的方法.分享给大家供大家参考,具体如下: 写一个函数,计算给定日期是该年的第几天. 满足闰年的条件: 闰年是公历中的名词,能被4整除但不能被 ...

  3. python gui界面设置数据储存在哪里_Python写GUI

    开头啰嗦 最近在想着学Python好像也很久了(从18年年底到现在,虽然中间断断续续),总是在反反复复学习语法,反反复复忘记语法.马克思原理告诉我们:"实践是检验真知的唯一原理." ...

  4. python用链表求两数之和_python 算法 - 008 计算两个链表所代表的整数之和 (整数相加法)...

    python 算法 - 008 计算两个链表所代表的整数之和 (整数相加法) 知之者不如好之者, 好之者不如乐之者.--<雍也> 知道德者不如好道德者, 好道德者不如乐道德者, 是为形容人 ...

  5. python输入出生年份、输出年龄_python根据出生日期计算年龄的代码

    python根据出生日期计算年龄的代码,运行后会提醒用户输出出生的年月日,然后输出年龄,可以改写为一个通用函数 from time import * #a function to find your ...

  6. python提取包含特定字符串的行_python如何提取字符串中的指定的内容?

    红颜莎娜 python读取文件内容的方法:一.最方便的方法是一次性读取文件中的所有内容并放置到一个大字符串中:all_the_text = open('thefile.txt').read( )# 文 ...

  7. python设计一个学生类求最高分_Python练习:计算每个学生的最低和最高分数。

    这里编程总初学者,需要你的指导.在 我正在从一些免费的在线课程中学习python,然后遇到了这个我已经解决了的特殊练习,但是让我困惑的是,有两种不同的方法会产生不同的结果.就这样..在 问题是: 使用 ...

  8. python计算时间装饰器_python 写一个计算运行时间的装饰器

    面试题之中的一个. 写一个装饰器wraps,它接收一个參数t.假设函数运行时间超过1秒,输出"bad",否则输出"goods". 首先,计算函数的运行时间: i ...

  9. python的输入来源包括网络输入法_python可以调用计算机上的输入法进行输入吗?比如调用输入法在其他程序的输入框中写上字符串...

    满意答案 baby_dmz 2015.10.29 采纳率:56%    等级:9 已帮助:1116人 "直接用输入法打出来的字" -- 这里含好几个过程. (1)当你用键盘打字时, ...

最新文章

  1. 梳理:python—同一个类中的方法调用
  2. [Oracle] CPU/PSU补丁安装教程
  3. revit模型怎么在手机上看_沙盘模型应该怎么看?一定要警惕这些问题
  4. 多线程的那点儿事(之无锁队列)
  5. [转载] 字符串的startsWith和endWith方法
  6. vue.js实现自定义输入分页
  7. linux删除文件、文件夹操作
  8. java反编译工具luyten增强版
  9. html选择时间区间控件,Html5添加用户选择一个日期时间范围的日期选择器插件教程...
  10. 打工人福利!教你如何一秒制作日报月报
  11. IPIP.net状告阿里云抄袭侵权
  12. css设置html背景颜色代码,CSS背景颜色
  13. 树莓派安装各种Ubuntu版本及系统推荐
  14. 【19】processing-硬件(中文)
  15. Docker 之 基操
  16. android10新功能,三星A80升级安卓10 更新One UI 2.0内容新功能介绍
  17. 苹果手机无法验证应用怎么办_苹果关闭iOS 14.0.1验证,用户将无法降级_
  18. 【专家访谈】性能架构师 - 贾江兵
  19. android手机裸眼3D技术原理和编程实现
  20. (记录)绝对值的实现

热门文章

  1. typedef 函数指针的用法
  2. python(numpy,pandas2)——numpy 运算
  3. python基本算法语句_Python中基本且又常用的算法
  4. java判断括号是否闭合_【python每日一练】有效括号
  5. Linux命令详解:./configure、make、make install 命令
  6. C/Cpp / 虚函数是否可以用 inline 修饰
  7. mmap 和 shm 区别
  8. C++11 std::bind 和 std::placeholder
  9. arraylist 后往前遍历_面试官:请说出线程安全的 ArrayList 有哪些,除了Vector
  10. vue 自己写上传excel组件_vue结合elementui组件 el-upload 上传excel表格(二)