Price Guard with @property
Python
30 pts
Price Guard with @property
Create a class ShopItem:
__init__(self, name, price)stores the name and setsself.price = price(this must go through the setter).- A
priceproperty that returns the privateself.__price. - A
pricesetter that saves the new value only if it is greater than 0; otherwise it printsPrice must be positiveand keeps the old price. - A read-only property
labelthat returnsName: price Nakfa, for exampleCoffee: 150 Nakfa.
You can assume the starting price is always valid. Expected output:
Price must be positive Coffee: 150 Nakfa
Checklist
- Define a class
ShopItemwith apriceproperty that returns the privateself.__price. - Add a
pricesetter that saves only prices greater than 0 and otherwise printsPrice must be positive. - Add a read-only property
labelin the formName: price Nakfa. - Run the code at the bottom.
🔑 Solution
If you view the solution before passing, you earn half points for this exercise. You still need to type it and press Check to finish.
How it works, step by step
Point at a step to highlight its lines. Type the solution yourself and press Check answer to finish.
