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 The Quiz Class

zakaria arr
zakaria arr
6,347 Points

keep getting 'question_types' type not defined?

this code keeps getting this error and yet it looks like what kenneth love did! any suggestion?

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.choice(1, 10)
        num2 = random.choice(1, 10)
        question = random.choice(question_types)(num1, num2)
      # add these questions into self.questions
        self.questions.append(question)
>>> quiz = Quiz()                                                                                                                        
Traceback (most recent call last):                                                                                                       
  File "<stdin>", line 1, in <module>                                                                                                    
  File "/home/treehouse/workspace/quiz.py", line 12, in __init__                                                                         
    question_types = (Add, Multiply)                                                                                                     
NameError: name 'question_types' is not defined   

Thanks in advance

[MOD: fixed formatting -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Relooking at Kenneth's code, the self. should not be necessary. There is something else not right with the code.

There is a syntax error in num1 = random.choice(1, 10). random.choice takes only one argument. I think you meant to use num1 = random.randint(1, 10). Is the code above your "final" version?

If you want to investigate further, please post your updated code and your questions.py file,

zakaria arr
zakaria arr
6,347 Points

thanks I've changed it and it work now

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Need to add self. in front of question_type:

class Quiz:
    questions = []
    answers = []

    def __init__(self):
        self.question_types = (Add, Multiply) #<-- added 'self'
zakaria arr
zakaria arr
6,347 Points

Thanks it works but how comes kenneth's code runs fine without self in front of question_types