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

Problem with quiz.py for the Quiz class video. I keep getting quiz_types is not defined.

'''Python

import datetime import random

from questions import Add, Multiply

class Quiz: questions = [] answers = []

def __init__(self):
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(questions)


def take_quiz(self):
    # log the start time
    # ask all of the questions
    # log if they got the question right
    # log the end time
    # show a summary
    pass

def ask(self, question):
    # log the start time
    # capture the answer
    # check the answer
    # log the end time
    # if the answer's right, send back True
    # otherwise, send back False
    # send back the elapsed time, too
    pass

def total_correct(self):
    # return the total # of correct answers
    total = 0
    for answer in self.answers:
        if answer[0]:
            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
    ))

'''

can you fix the formatting so I can tell where the indentations are?

Steven Parker
Steven Parker
243,656 Points

Those 3 marks that define code are accents, not apostrophes.

` :point_left: not :point_right: '

Also, the identifier "quiz_types" does not appear in this code, but the code seems to be dependent on another module named "questions.py" that is not shown here.

To share the complete project (without need for special formatting), make a snapshot of your workspace and post the link to it here.

Below is my snapshot of my workspace. https://w.trhou.se/kipdy2osue

1 Answer

Steven Parker
Steven Parker
243,656 Points

Both the "quiz.py" and "questions.py" modules in the snapshot only define classes, they don't contain any code to create instances of the classes or any other functional code. Running "python quiz.py" just exits with no errors.

Are you sure this is a snapshot of your current code, and what do you do to see this error?

Sorry, I really didn't explain my problem. From the video Kenneth ran the program to test it by putting in the console

from quiz import Quiz quiz = Quiz() The program runs. However, when I run my I get a Traceback stating: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/treehouse/workspace/quiz.py", line 17, in init
question = random.choice(question_types)(num1, num2)
NameError: name 'question_types' is not defined

Steven Parker
Steven Parker
243,656 Points

Aha, the error is not "quiz_types" that is not defined, but "question_type".

So on line 17 there appears to be three errors:

          question = random.choice(question_types)(num1, num2)
  • instead of "question_types" it should be "self.question_types".
  • instead of "question" (singular) it should probably be "questions" (plural).
  • and the "(num1, num2)" doesn't seem to belong there (or do anything).