Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dan A
Courses Plus Student 4,036 Pointsfunctional programing solution
During the "Quiz Class" video, Kenneth mentioned a "functional programing" solution for determining the total_correct. Does anyone know what this solution might be? I came up with (I think) a list comprehension solution. Is this functional programming?
return len([ans for ans in self.answers if ans is True])
And, as a side question is my list comp solution any better than
total = 0
for ans in self.answers:
if ans is True:
total += 1
return total
??
Thanks
1 Answer
Kenneth Love
Treehouse Guest TeacherList comprehensions are similar to functional programing but lack a...function ;) That said, yeah, that's an acceptable way.
Since our list only contains True and False, you can actually do ti with:
total = len(list(filter(None, self.answers)))
We actually just filmed a workshop on functional programming in Python that'll be out on the site soon. It may be Pro-only, though, I'm not sure.
Gergő Bogdán
6,664 PointsGergő Bogdán
6,664 PointsI think your first solution in syntax is similar to functional programs, but its not an actual functional program :)
I think both of your solutions are OK, but are not the same. In the second example you are simply counting the true values, not counting the length of a list in which you added only True elements.