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 trialEldin Guzin
6,010 PointsI 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
231,236 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
6,010 PointsEldin Guzin
6,010 PointsThanks for the great answer Steven
Spencer Hurrle
3,128 PointsSpencer Hurrle
3,128 PointsSteven 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
231,236 PointsSteven Parker
231,236 PointsThe 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.