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 trial

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

Julie Pham
Julie Pham
12,290 Points

Why we need log the start_time and end_time in ask method?

Why we need log the start time and end time in ask method while we already have log the start time and end time in take_quiz method?

3 Answers

Hanley Chan
Hanley Chan
27,771 Points

Hi,

The time logged in the ask method should be for the time used for each question and the take_quiz method should be the time for the whole quiz.

Yes, but they are unused. I was able to comment them out and the the script runs just fine. I did have to change to use True instead of [0] as in the original file.

        for answer in self.answers:
            if answer == True:
import datetime
import random

from questions import Add, Multiply


class Quiz:
    questions = []
    answers = []

    def __init__(self):
        question_types = (Add, Multiply)
        # generate 10 random questions with numbers from 1 to 10
        for _ in range(10):
            num1 = random.randint(1, 10)
            num2 = random.randint(1, 10)
            question = random.choice(question_types)(num1, num2)
            # add these questions into self.questions
            self.questions.append(question)

    def take_quiz(self):
        # log the start time
        self.start_time = datetime.datetime.now()

        # ask all of the questions
        for question in self.questions:
            # log if they got the question right
            self.answers.append(self.ask(question))
        else:
            # log the end time
            self.end_time = datetime.datetime.now()

        # show a summary
        return self.summary()


    def ask(self, question):
        correct = False
        # log the start time
        #question_start = datetime.datetime.now()

        # capture the answer
        answer = input(question.text + ' = ')

        # check the answer
        if answer == str(question.answer):
            correct = True

        # log the end time
        #question_end = datetime.datetime.now()

        # if the answer's right, send back True
        # otherwise, send back False
        # send back the elapsed time, too
        return correct #, question_end - question_start

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

    def summary(self):
        # print how many you got right and the total # of questions. 5/10
        print("You got {} out of {} right.".format(
                self.total_correct(), len(self.questions)
        ))
        # print the total time for the quiz: 30 seconds!
        print("It took you {} seconds total.".format(
                (self.end_time-self.start_time).seconds
        ))


Quiz().take_quiz()

If a person wanted to access those variable how would they do it? ask.start_time, ask.end_time?

I think having those variables would be useful if you decided to work on one of his suggestions -> to give the time it took to answer each question. However, it definitely added unnecessary complication to the original challenge. Especially when you are trying to figure out how you're supposed to use those variables when you're working on it.