Rather, it receives a collection or an array of vectors, each of which contains objects. The function then iterates through these vectors and accesses the objects contained within them to perform its intended operation. For example:
def total_price(items):
total = 0
for item_set in items:
for item in item_set:
total += item.price
return total
# Usage
items = [[Item("apple", 1.50), Item("banana", 0.75)], [Item("orange", 2.00)]]
print(total_price(items)) # prints 4.25
In this example, the total_price
function receives a list of two lists, each of which contains Item
objects. It iterates through both lists and calculates the total price by accessing the price
attribute of each Item
object. The final result is returned.
Asked: 2023-04-29 14:37:34 +0000
Seen: 8 times
Last updated: Apr 29 '23