利用Python函數(shù)實現(xiàn)一個萬歷表完整示例
大家可以根據(jù)格式化打印字符去調(diào)一下最后的輸出,不過有中文好像不好調(diào)整,可以換成星期的單詞,這樣應該會好一點,format()函數(shù)可以用來格式化打印字符,format()可以使用字符串去調(diào)用,也可以獨自使用。
可以點進格式化打印字符了解一下哦
示例代碼# 判斷是否閏年def isleap(year): return year % 4 == 0 and year % 100 != 0 or year % 400 == 0# 判斷月的天數(shù)def month_days(year,month): if month in [1,3,5,7,8,10,12]: return 31 if month == 2: if isleap(year): return 29 else: return 28 return 30# 1900年到輸入年份的總天數(shù)def total_days(year): s = 0 for i in range(1900,year): if isleap(i): s += 366 else: s += 365 return s# 1月到輸入月份的天數(shù)def days(year,month): s = 0 for i in range(1,month): s += month_days(year,i) return s# 獲取某年某月的日歷def monthcalendar(year,month): total = total_days(year) + days(year, month) a = total % 7 print(’星期日’.center(8, ’ ’), end=’’) print(’星期一’.center(8, ’ ’), end=’’) print(’星期二’.center(8, ’ ’), end=’’) print(’星期三’.center(8, ’ ’), end=’’) print(’星期四’.center(8, ’ ’), end=’’) print(’星期五’.center(8, ’ ’), end=’’) print(’星期六’.center(8, ’ ’), end=’’) print() count = 0 for i in range(0, month_days(year, month) + a + 1): if i <= a: print(format(’ ’,’10’), end=’’) count += 1 else: print(format(str(i - a),’^10’), end=’’) count += 1 if count == 7: count = 0 print() print()# 輸出某年一年的日歷def yearcalendar(year): for i in range(1,13): print(f’{i}月:’) monthcalendar(year,i) print()# 開始函數(shù)def start(): while True: print(’-------歡迎來到萬歷表查詢頁面-------’) print(’1.查詢某年的日歷n2.查詢某年某月的日歷n3.退出查詢’) print(’---------------------------------’) n = int(input(’請輸入你的操作:’)) if n == 1: year = int(input(’請輸入要查詢的年份:’)) yearcalendar(year) elif n == 2: year = int(input(’請輸入要查詢的年份:’)) month = int(input(’請輸入1-12:’)) monthcalendar(year,month) elif n == 3: print(’退出成功’) break else: print(’指令錯誤,請重新輸入!!!’)if __name__ == ’__main__’: start()總結(jié)
到此這篇關(guān)于利用Python函數(shù)實現(xiàn)一個萬歷表的文章就介紹到這了,更多相關(guān)Python函數(shù)實現(xiàn)萬歷表內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. PHP正則表達式函數(shù)preg_replace用法實例分析2. 一個 2 年 Android 開發(fā)者的 18 條忠告3. vue使用moment如何將時間戳轉(zhuǎn)為標準日期時間格式4. js select支持手動輸入功能實現(xiàn)代碼5. Android 實現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進程6. Android studio 解決logcat無過濾工具欄的操作7. 什么是Python變量作用域8. vue-drag-chart 拖動/縮放圖表組件的實例代碼9. Spring的異常重試框架Spring Retry簡單配置操作10. Vue實現(xiàn)仿iPhone懸浮球的示例代碼
