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 Advanced Objects Special Methods

Whats wrong here

I'm getting an error saying that the class circle has no attribute called radius here's my code

class Circle:
# create a class called circle
    def __init__(self, diameter):
        # creates an init that takes self and diameter
        self.diameter = diameter
        # When run this will cause whatever value put in for diameter to be the diameter
        @property
        # Whatever method put here will be a property of the circle class
        def radius(self):
            self.radius = radius
        # define a new method called self
            return self.diameter / 2
        # this will return the diameter divided by to (or the radius)
small = Circle(10)
print(f"The diameter of the circle is {small.diameter}")
print(f"The radius of the circle is {small.radius}")

1 Answer

Hi Aizah

I tested your code and modified it a bit, now it works:

class Circle:
# create a class called circle
    def __init__(self, diameter):
        # creates an init that takes self and diameter
        self.diameter = diameter
        # When run this will cause whatever value put in for diameter to be the diameter
    @property
    # Whatever method put here will be a property of the circle class
    def radius(self):
    # define a new method called self
        return self.diameter / 2
    # this will return the diameter divided by to (or the radius)
small = Circle(10)
print(f"The diameter of the circle is {small.diameter}")
print(f"The radius of the circle is {small.radius}")

I did the following:

  • indented the radius method correctly
  • deleted this line self.radius = radius

This is the output I got:

The diameter of the circle is 10
The radius of the circle is 5.0

You can test it out yourself!