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
akshat mehrotra
2,727 Pointswhy isn't my code working
import random
class Question:
text = None
value = None
num_1 = random.randint(1,10)
num_1 = random.randint(1, 10)
class addition(Question):
def __init__(self):
self.text = "{} + {}".format(self.num_1, self.num_2)
self.value = self.num_1 + self.num_2
when ever I run this code I get the error saying "builtins.AttributeError: 'addition' object has no attribute 'num_1' " I don't understand why It is saying that?? class addition is a sub class of Question and num_1 is defined in class Question HELP
[MARKDOWN ADDED BY MOD]
1 Answer
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Akshat
I ran your code with minor changes and got no errors.
import random
class Question:
text = None
value = None
num_1 = random.randint(1,10)
num_2 = random.randint(1, 10) # changed num1 to num2
class addition(Question):
def __init__(self):
self.text = "{} + {}".format(self.num_1, self.num_2)
self.value = self.num_1 + self.num_2
def __str__(self): # added a __str__ method
return str(self.value)
a = addition()
print(a)
# first time i ran it i got 12 and the second time 11
# your error was a bit strange,
#it should have said "builtins.AttributeError: 'addition' object has no attribute
# 'num_2' " because your were refering to a self.num2 that was not declared.
hope this helps