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

Python Dates and Times in Python (2014) Let's Build a Timed Quiz App Taking The Quiz

Alex Tirrell
Alex Tirrell
3,359 Points

count() is finding one too many correct answers

In order to find how many questions were answered correctly, I'm flattening my list of answer information and counting the number of True values in it. The flattened list is:

[35, True, datetime.timedelta(0, 3, 120616), 12, True, datetime.timedelta(0, 1, 348287), 6, False, datetime.timedelta(0, 1, 561616), 3, True, datetime.timedelta(0, 2, 859576), 3, True, datetime.timedelta(0, 1, 238063), 6, False, datetime.timedelta(0, 2, 471605), 4, True, datetime.timedelta(0, 2, 488742), 8, True, datetime.timedelta(0, 1, 910301), 80, True, datetime.timedelta(0, 1, 920059), 1, True, datetime.timedelta(0, 2, 17969)]

Counting the Trues in the list, you'll see there are eight. However, if I perform a .count(True) on the list, it returns 9. What's going on?

1 Answer

Steven Parker
Steven Parker
231,248 Points

:point_right: Your list includes the value 1, and 1 == True.

One way around this would be to get a count based on identity instead of equality using a comprehension:

truecount = len([1 for x in flatlist if x is True]))
Alex Tirrell
Alex Tirrell
3,359 Points

Okay, I think I understand. I had thought about that as an issue, but I thought that python considered any integer above 0 as True, in which case there should have been a lot more false positives. Based on your answer, I'm assuming that "truthy" and "True" are not the same thing. I'm still not clear, though, in which cases any positive number will count as true, and in which cases it will only count "True" and "1". Can you help me out with that?