Python Dates and Times in Python Let's Build a Timed Quiz App The Quiz Class

Matthew Hill
Matthew Hill
7,799 Points

What 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
Steven Parker
203,990 Points

If 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.

:point_right: by testing answer[0], you accumulate a total of only the correct answers.

Matthew Hill
Matthew Hill
7,799 Points

Hi 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.