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 trialMatthew Hill
7,799 PointsWhat is the purpose of the index in - if answer[0] == True: ?
I wrote my program before watching the video and used this code instead:
<p>
right = 0
for answer in self.answers:
if answer:
right += 1
</p>
It provides me with an accurate count so I'm wondering why Kenneth added an index in his code? Thanks, Matt
1 Answer
Steven Parker
231,236 PointsIf you just test answer, you're really just checking to see if the list is empty (which it is not, or you would not be inside the loop at all). You would not need a loop for that, you could just use: len(answers)
.
So you might get the same result as using the index, but only when every answer is correct. But if any answer is wrong, the result will be different.
by testing answer[0], you accumulate a total of only the correct answers.
Matthew Hill
7,799 PointsMatthew Hill
7,799 PointsHi Steven, thanks for responding!
I think you misunderstood me, my code will show the correct number of answers (n/10) each time. Whether this is 10, 0 or anything in between. As I'm looping through each one the ' if answer: ' line will continue to the next line only if the thing it is looping over is truthy. As I'm looping over a list containing only Booleans I still don't understand the use of the index.