python日历提醒_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 打印日歷
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字符集: ...
隨機推薦
JS-DOM對象知識點匯總(慕課)
D ...
echarts基本使用
基本操作: 1,準備好需要渲染chart圖的div層
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python日历提醒_python打印日历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 董卿退出央视的原因(央视董卿丑闻)
- 下一篇: 有线电视故障报修电话