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

Eldin Guzin
Eldin Guzin
2,504 Points

I have a question about one part of this video

def total_correct(self):
        # return the total # of correct answers
        total = 0
        for answer in self.answers:
            if answer[0]:
                total += 1
            return total

how does this exactly work, this is extremely confusing, what is self.answers exactly, if answer[0] , doesn't that always check the first answer, doesn't really make sense to me. An explanation would be very helpful here.

1 Answer

Steven Parker
Steven Parker
201,913 Points

"answers" (plural) is the whole list, but "answer" (singular) is a tuple where the first part is a boolean indicating whether it was correct or not. So "answer[0]" is that boolean value.

That means this loop is going through the list and counting up all the correct answers.

Eldin Guzin
Eldin Guzin
2,504 Points

Thanks for the great answer Steven

Spencer Hurrle
Spencer Hurrle
2,910 Points

Steven Parker Is this true for the variables in all for loops? Could you point me to where I could read more about that in documentation??

Steven Parker
Steven Parker
201,913 Points

The loop variable (after "for") will always be one item (at a time) from the iterable (that comes after "in").
For more details, see for statements in the official documentation.