from datetime import date, timedelta happy_day_count = 0 td = timedelta(days=1) d = date(2015, 1, 1) while d.year == 2015: digit1 = d.day % 10 # 1の位 digit2 = int((d.day/10) % 10) # 10の位 if d.month == digit2 + digit1: happy_day_count += 1 # 翌日に進める d = d + td print(happy_day_count)