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

Adam Siwiec
Adam Siwiec
12,070 Points

OOP question

Why do I get an NameError on numberCorrect for this class/method:

from questions import GenerateQuestion

class Quiz:
    numberCorrect = 0

    def take_quiz(self):
            GenerateQuestion(self.numberCorrect,"+")

Here is my GenerateQuestion class:

import random
class GenerateQuestion:
     text = None
     answer = None
     num1 = None
     num2 = None
     maxnum = 0
     def __init__(self, scale, symbol):
         self.maxnum = 2 ** scale
         self.num1 = random.randint(1,self.maxnum)
         self.num2 = random.randint(1,self.maxnum)
         self.text = "{} {} {}".format(self.num1,symbol,self.num2)
         if symbol == "+":
             self.answer = self.num1 + self.num2
         if symbol == "-":
             self.answer = self.num1 - self.num2
         if symbol == "*":
             self.answer = self.num1 * self.num2
         if symbol == "/":
             self.answer = self.num1 // self.num2
Wairton Rebouças
Wairton Rebouças
8,225 Points

I've tested your code, it seems to be working fine on both python 2 and 3 on my machine. This is part of some quiz? If you are running this on your computer, what error message you got?