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 Object-Oriented Python Dice Roller Project Breakdown

Haifa Bassam
Haifa Bassam
10,333 Points

AttributeError

Each time I try to write the code d.value in my console I get an error that says: " 'Die' object has no attribute 'value'

This is my code:

import random 

class Die:
  def __init__(self, sides=2, value=0):
      if not sides >= 2:
          raise ValueError("Must have at least 2 sides")
      if not isinstance(sides, int):
          raise ValueError("Sides must be a whole number")
          self.value = value or random.randint(1, sides) 

and this is what i code into the console:

from dice import Die
d = Die()
d.value
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Die' object has no attribute 'value '

2 Answers

Steven Parker
Steven Parker
231,248 Points

The line that creates self.sides is indented more than the video example. This causes it to be controlled by the "if" statement and located after the "raise" statement (which will prevent any code following from being executed).

Shift the indentation one stop left to get the behavior shown in the video.

Haifa Bassam
Haifa Bassam
10,333 Points

ok that worked! I read the code hundred time and I don't know how I missed that simple error :0!

Thank you a lot..