2.1k views
1 vote
Given the following code snippet, what statement completes the code to add several items to one grocery list:def addItems (self, price, quantity, itemName) :for i in range(quantity) :def addItem(self, price, itemName) :self._itemCount = self. ItemCount + 1self._totalPrice = self._totalPrice + pricea. self.addItem(price, itemName)b. addItem(price, itemName)c. self._addItem(price, itemName)d. addItem(price, itemName)

User Rivero
by
9.0k points

1 Answer

3 votes

Answer:

The correct statement to complete the code and add several items to one grocery list is:

a. self.addItem(price, itemName)

Step-by-step explanation:

This statement calls the addItem() method of the object itself (self) with the specified price and itemName arguments. Since the addItem() method increases the itemCount and updates the totalPrice of the grocery list, this allows for adding multiple items to the list based on the specified quantity argument passed to the addItems() method.

Therefore, the complete code with the correct statement would look like this:

class GroceryList:

def __init__(self):

self._itemCount = 0

self._totalPrice = 0.0

def addItem(self, price, itemName):

self._itemCount = self._itemCount + 1

self._totalPrice = self._totalPrice + price

def addItems(self, price, quantity, itemName):

for i in range(quantity):

self.addItem(price, itemName)

Note that the addItem() method is defined within the GroceryList class and updates the private attributes _itemCount and _totalPrice of the grocery list object. The addItems() method uses a loop to call the addItem() method quantity number of times to add multiple items to the grocery list.

User Snouto
by
8.0k points