日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2024/10/8 python 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python怎么调出某年某月日历_Python 写的计算指定年指定月日历的脚本 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天初學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 写的计算指定年指定月日历的脚本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。