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

Kabir Gandhiok
Kabir Gandhiok
12,553 Points

In the method ask(self, question), how can question reference text, it isnt an instance?

I cannot clearly understand how in the method ask(self, question) does question reference text and answer, it isnt an instance. Now I understand that question is passed in from the take_quiz(self) method but can anyone help me understand how it gets to climb up the inheritance tree?

Thanks!

1 Answer

Boban Talevski
Boban Talevski
24,793 Points

The question object passed in to the method ask is indeed an instance of the class Question, or actually of one of its children. I assume you missed this part in the init method in class Quiz:

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) # <-- this right here instantiates a new Add or Multiply object, both of the classes inheriting the Question class, with inherited answer and text variables
        # add these questions into self.questions
        self.questions.append(question) # <-- here we 'feed' those Question instances in the questions list

And the questions list is used in take_quiz method, in which each question of the said list is passed to the ask method. So that's how the question that ends up in the ask method is actually an instance of a child of the Question class and has access to both text and answer. Hopefully it clears things up.