Cute_ Panda 🐼 就業了,Still 窮。 says to OwO
year, month = 2021,5 print('SU\tMO\tTU\tWE\tTH\tFR\tSA') # Print 上面那條 _month = { # 列舉一個月有幾天 # 月:幾天 1: 31, 2: [28, 29], 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31 } # 列舉平年, 閏年天數 _year = [365, 366] def isLeap(year): # 一個判斷是不是閏年的 Method if year % 400 == 0: return True if year % 100 == 0: return False if year % 4 == 0: return True # 這裡算出總共幾天,除以7的餘數就是月初所在的位置。 total_days = 0 for __year in range(1, year + 1): # 假設是2021年03月算年的部份應該算1-2021年。 total_days += _year[1] if isLeap(__year) else _year[0] for __month in range(1, month): # 這裡算到02月底就可以,因為要顯示的是三月的開頭。 if __month == 2: if isLeap(year): total_days += _month[2][1] else: total_days += _month[2][0] else: total_days += _month[__month] _start = total_days % 7 # 顯示的月開始要跳過幾個格子 _end = _month[month] if month != 2 else _month[2][1] if isLeap( year) else _month[2][0] # 顯示的月有幾天 # 輸出結果 _str = str() _day = 1 _count = 0 for _ in range(_start + _end): if _ < _start: _str += '\t' else: _str += str(_day) + '\t' _day += 1 _count += 1 if _count == 7: print(_str) _count = 0 _str = str() print()