- The assumption of this practice is you already have supermarket_item.py with SupermarketItem class in it.
- Create a new python file supermarket_practice.py
- Inside the file, create a new class SupermarketPractice
- Inside the class, create an __init__ method.
- ‘Inside the method create a new class attribute: items_dictionary. Remember since this is a class attribute, you can to prefix with self. This class attribute will be an empty dictionary.
- import SupermarketItem class on the top of the file. Look at how we did it in supermarket_system.py.
- Create a new method called “initialize_items“. A method is a function inside a class. Since it is a method, it needs to have “self” as the first parameter.
- Inside this new method initialize_items add 4 new SupermarketItems into items_dictionary class attribute. Make the name of the item as the key and SupermarketItem as the value.
- Apple Juice, $10, Frozen is False.
- Banana, $3, Frozen is False.
- Fish, $10, Frozen is True.
- Frozen Pizza, $20, Frozen is True.
- Create a new method, list_affordable_items . Inside this method, loop through each element inside the items_dictionary class attributes and list all items whose price is $10 or less. If you are struggling, look at how we did list_all_teachers in SchoolApplication project. So expected items would be
$10 or less items:
Apple Juice - $10
Banana - $3
Fish - $10
- Create an instance of Practice class and call initialize_items methods and then list_affordable_items method. Something like this
sp = SupermarketPractice()
sp.initialize_items()
sp.list_affordable_items()