import datetime aDate = datetime.date(2015, 1, 1) b = 0 while aDate != datetime.date(2015,12,31): digits = [int(c) for c in str(aDate.day)] if aDate.month == sum(digits): b = b + 1 aDate = aDate + datetime.timedelta(days=1) print b