fork download
  1. def get_week_from_yyyymmdd(y, m, d):
  2. ''' 年月日から曜日を算出
  3. 1582年~(グレゴリオ暦)に限る
  4.  
  5. 戻り値 int
  6. 0: 日曜日
  7. 6: 土曜日
  8. '''
  9.  
  10. # 1月、2月は、前年の13月、14月として扱う
  11. if m <= 2:
  12. y -= 1
  13. m += 12
  14. # print('{}年{}月{}日'.format(y, m, d))
  15.  
  16. x = y + y // 4
  17. x -= y // 100
  18. x += y // 400
  19. x += (m * 13 + 8) // 5
  20. x += d
  21. w = x % 7
  22.  
  23. weeks = '日月火水木金土'
  24. # print('{}曜日'.format(weeks[w]))
  25.  
  26. return w
  27.  
  28.  
  29. def make_days_in_months(year):
  30. def is_leapyear(y):
  31. ''' うるう年判定
  32. '''
  33. result = False
  34. if y % 4 == 0:
  35. result = True
  36. if y % 100 == 0 and y % 400 != 0:
  37. result = False
  38. return result
  39.  
  40. days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  41. if is_leapyear(year) is True:
  42. days_in_months.remove(28)
  43. days_in_months.insert(1, 29)
  44. return days_in_months
  45.  
  46.  
  47. def main(year, month):
  48. days_in_months = make_days_in_months(year)
  49. days = list(range(1, days_in_months[month - 1] + 1))
  50.  
  51. day = 1
  52. week = get_week_from_yyyymmdd(year, month, day)
  53. dumy = [0] * week
  54. days = dumy + days
  55.  
  56. print()
  57. print(' {}年{}月'.format(year, month))
  58. print('---------------------')
  59. print(' 日 月 火 水 木 金 土')
  60. print('---------------------')
  61. for count, i in enumerate(days, start=1):
  62. if i == 0:
  63. print(' ', end='')
  64. continue
  65. print(' {:2}'.format(i), end='')
  66. if count % 7 == 0 and i != days[-1]:
  67. print('')
  68. print('\n---------------------')
  69.  
  70.  
  71. if __name__ == '__main__':
  72. while True:
  73. y = int(input('Year (1582~): '))
  74. if 1582 <= y:
  75. break
  76. while True:
  77. m = int(input('Month: '))
  78. if 0 < m < 13:
  79. break
  80. main(y, m)
  81.  
Runtime error #stdin #stdout #stderr 0.02s 9336KB
stdin
Standard input is empty
stdout
Year (1582~): 
stderr
Traceback (most recent call last):
  File "./prog.py", line 73, in <module>
EOFError: EOF when reading a line