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

Dieter Bauwens
Dieter Bauwens
4,176 Points

Problem with task objectoriented-python-2/advanced-objects/setters

Hi,

Wanted to see whether this is a bug or not. Running my code properly sets all attributes, so not sure why it’s failing with a message “Didn't correctly set the @price property”.

class Product: _price = 0.0 tax_rate = 0.12

def __init__(self, base_price):
    self._price = base_price

@property
def price(self):
    return self._price + (self._price * self.tax_rate)

@price.setter
def price(self, price):
    self._price = price / (1 + self.tax_rate)

Thank you in advance for your feedback.

Steven Parker
Steven Parker
229,670 Points

Could you provide a link to the course page this is on?

5 Answers

Steven Parker
Steven Parker
229,670 Points

You're updating the base price (_price) here.

So, unlike the property method that returns the price with tax calculated, the setter method does not need any calucation. Just copy the argument into the base price.

Dieter Bauwens
Dieter Bauwens
4,176 Points

hi Steven,

it's not that I don't believe you, and yes, I changed the code as you indicated and it passed, but honestly, this does not make sense at all,

running the code that passed does not return a logical result:

class Product: _price = 0.0 tax_rate = 0.12

def __init__(self, base_price):
    self._price = base_price

@property
def price(self):
    return self._price + (self._price * self.tax_rate)

@price.setter
def price(self, price):
    self._price = price #price / (1 + self.tax_rate)

prod = Product(100) print 'prod._price:', prod._price print 'prod.price:', prod.price prod.price = 200 print 'prod._price:', prod._price print 'prod.price:', prod.price

if you set the price via the setter, then this should update the base price deducting the tax rate from the new price, just as Kenneth showed in the video with a similar example,

anyway,

thank you for getting back to me on this topic and for the feedback provided,

cheers, Dieter

Steven Parker
Steven Parker
229,670 Points

Now that you know how to pass the challenge, if you feel the challenge has a bug, the instructions for bug reporting are on the Support page.

Dieter Bauwens
Dieter Bauwens
4,176 Points

hi Steven,

the way I read it, you need to update the price, not the base price, hence the price you set is including tax (which does not require a calculation, I agree), this would mean though that when you set the price you need to update the base price since that would change implicitly,

in the video before, Kenneth is doing a similar exercise, his setter also updates "a base element",

thank you, Dieter

Steven Parker
Steven Parker
229,670 Points

No need to take my word for it. leave off the calculation and see if you pass.

    self._price = price
Dieter Bauwens
Dieter Bauwens
4,176 Points

ok, thank you for that note, see you around, Dieter

Jacinto Jacinto
seal-mask
.a{fill-rule:evenodd;}techdegree
Jacinto Jacinto
Python Web Development Techdegree Student 7,869 Points

I actually just actually did this exercise. I just passed it because I looked up an answer(the first time i did so for a quiz), but it really doesn't make sense to me at all. I wish someone could give a good explanation why this works:

@price.setter def price(self, price): self._price = price

and this doesn't:

@price.setter def price(self, price): self._price = price / (1 + self.tax_rate)

for some reason we don't have to specify or modify anything for this example. We just use the parameter's unadulterated value and it magically gives us the price before tax. I mean I wouldn't make such a big deal out of it if it weren't for the fact that there is a huge issue of consistency here. In the video just before this we had to specify that self.diameter was equal to the radius MULTIPLIED by 2.

@radius.setter def radius(self, radius): self.diameter = radius * 2.

with the logic of how we solved the quiz this should work perfectly:

@radius.setter def radius(self, radius): self.diameter = radius

But it doesn't. Am I missing something here?

Steven Parker
Steven Parker
229,670 Points

The challenges don't exactly replicate the video examples, I'd bet this is intentional. It's not inconsistent, the challenge instructions have different requirements.