Question: Create and use a python CLass to structure the data for the items in your catalogue. Create multiple instances of youtr new Class object which
Create and use a python CLass to structure the data for the items in your catalogue. Create multiple instances of youtr new Class object which represent the items in your catalogue and store them in a list. For example:
item1 = My_Item_Class(widget, something, data)
item2 = My_Item_Class(thingy, content, info)
my_catalogue_list = [item1, item2]
sol54:
Here is an example of how you can create a Python class to structure the data for the items in a catalogue:
class CatalogueItem:
def __init__(self, name, description, price):
self.name = name
self.description = description
self.price = price
This class has three attributes: name, description, and price. You can create an instance of this class for each item in your catalogue and store them in a list like this:
item1 = CatalogueItem('Widget', 'A small gadget for doing something', 9.99)
item2 = CatalogueItem('Thingy', 'A tool for organizing content', 19.99)
item3 = CatalogueItem('Gizmo', 'An electronic device with many functions', 49.99)
catalogue = [item1, item2, item3]
Now you have a list called catalogue that contains three instances of the CatalogueItem class. You can access the attributes of each item like this:
print(catalogue[0].name) # Output: Widget
print(catalogue[1].description) # Output: A tool for organizing content
print(catalogue[2].price) # Output: 49.99
You can also loop through the catalogue list to print the details of each item:
for item in catalogue:
print(item.name, '-', item.description, '- $', item.price)
This would output:
Widget - A small gadget for doing something - $ 9.99
Thingy - A tool for organizing content - $ 19.99
Gizmo - An electronic device with many functions - $ 49.99
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
