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

Patrick Lannan
Patrick Lannan
4,792 Points

Understanding question_start/end

I was playing around with deconstructing the quiz we set up in the lesson and got rid of the question_start and question_end in my ask method. It seemed like it wouldn't effect anything since I wasn't keeping track of how long it took to complete each individual question, and that was the only difference in my code from the version that just worked. When I ran my code, it displayed the quiz and I was able to input all of the answers. But once it came time to be given my summary, the code crashed because it said if answer[0]: "'bool' object is not subscript able," referring to the total_correct method. What does that mean and why would that happen by removing the question_start/end and keeping everything else the same.

2 Answers

Steven Parker
Steven Parker
202,109 Points

It sounds like you were expecting "answer" to be an array, but at some point in the code it was assigned with a true/false value instead.

If you'd like help finding the specific cause, show your code here. Even better, make a snapshot of your workspace and post the link to it here.


UPDATE: After seeing the snapshot, the "ask" method originally returns a tuple containing the result (a Bool) and the time taken. If you just removed the time taken from the "return" then taking the quiz would fill "answers" with Bool values instead of tuples. So later in "total_correct", when the loop tries to select the result part of what it expects to be a tuple by applying a subscript ("answer[0]"), you get the error "'bool' object is not subscriptable,"

So to completely remove the timing, you need to change every place where "answers" is accessed to treat it as a list of results instead of a list of tuples (with results and times).

Or a quick fix would be to just make sure "ask" still returns a tuple even if you don't care about the time:

        return correct, None  # return a tuple but with no time
Patrick Lannan
Patrick Lannan
4,792 Points

https://w.trhou.se/acujrookp2 Here's a snapshot. This should work fine, but once I remove the question_start and question_end from the ask method, the quiz finishes with the bool value not subscriptable error. All this is in the quiz.py by the way