import calendar dateList = [] for i in range(1, 13): first, date = calendar.monthrange(2015, i) dateList.append(date) month = 0 happyDays = 0 for days in dateList: month += 1 for date in range(1, days): if date < 10: if month == date: happyDays += 1 else: if month == sum(map(int, str(date))): happyDays += 1 print happyDays