Python Dates and Times in Python 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 59,892 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 59,892 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