import datetime ans = 0 now_date = datetime.datetime(2015, 1, 1) end_date = datetime.datetime(2015, 12, 31) while True: month = now_date.month day = now_date.day digit_sum = eval("+".join(str(day))) if month == digit_sum: ans += 1 now_date += datetime.timedelta(days=1) if now_date > end_date: break print(ans)