import calendar c = calendar.Calendar() happy_days = 0 def is_happy_day(month, day): if month == day // 10 + day % 10: return True else: return False for month, m_list in enumerate(c.yeardayscalendar(2015, width=12)[0], start=1): for w_list in m_list: for day in w_list: if day == 0: continue if is_happy_day(month, day): happy_days += 1 print(happy_days)