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

Daniel Petrov
Daniel Petrov
3,495 Points

Question not defined

I am trying something slightly different here. Putting the loop in the ask method instead. But when I try to run the ask(question) within the take_quiz(self) it returns error: question not defined. If I try with ask(self, question) it returns error: ask not defined. Also still confused with where do you need self. and where not. For example there is self.quiz_startTime but question_startTime?

import datetime
import random

from questions import Add, Multiply


class Quiz:
    questions = []
    answers = []

    def __init__(self):
        # question_types = (Add, Multiply)
        count = 0
        # Generate 10 random questsions (5 of each type)with numbers from 1 to 10
        while count < 10:
            num1 = random.randint(1,10)
            num2 = random.randint(1,10)
            question_add = Add(num1, num2)
            self.questions.append(question_add)
            count += 1
            num1 = random.randint(1,10)
            num2 = random.randint(1,10)
            question_mult = Multiply(num1, num2)
            self.questions.append(question_mult)
            count += 1
        # add these questions into self.questions

    def take_quiz(self):
        total_correct = 0
        # log the start time
        self.quiz_startTime = datetime.datetime.now()
        # ask all the questions
        self.ask(question)
        # log if they got the question right

        # log the end time
        self.quiz_endTime = datetime.datetime.now()
        # show a summary
        return summary(self)

    def ask(self, question):
        # log the start time
        question_startTime = datetime.datetime.now()
        # capture the answer
        for question in self.questions:
            answer = input("Question No {}: {}".format(index.self.questions(question), question.text) + ("\nYour answer: "))
        # check the answer
        # if the answer is right send back True
            if answer == question.answer:
                answer = True
                total_correct += 1
            # otherwise send back False
            else:
                answer = False
            self.answers.append(answer)
        # log the end time
            question_endTime = datetime.datetime.now()

        # send back the elapsed time, too
            elapsed_time = (question_endTime - quiz_startTime).seconds
            print ("Your time so far is {} secobds.".format(elapsed_time))


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

Quiz().take_quiz()

1 Answer