I have this dict with my values for some products which can be added to the cart. I want to sum the prices of all products in the cart and print them. How Do I select the specific value (price) from the dict using a function. In this instance ShoeA should be two times in the cart which should result in 144.98 + 144.98 = 289.96
Here is my complete code:
shoeA = {"id": 342453, "name": "Shoe A", "price": 144.98 }
shoeB = {"id": 9800989, "name": "Shoe B", "price": 300}
# A cart of a shop has the following functions. Each functionality should be implemented as a separate function.
# 1.1 add_product(cart, product, times): adds a product "times" times to the cart. The cart is a list and the product a
# dictionary. A product consists of a ID, name and price
cart = []
def add_product(product, times):
for i in range(0, times):
cart.append(product)
print(cart)
add_product(342453, 4)
# 1.2 remove_product(cart, productID): Removes all products with ID = productID from the given cart
# 1.3 print_cart(cart): Prints the whole cart
def remove_product(productID, times):
for i in range(0, times):
cart.remove(productID)
print(cart)
remove_product(342453, 2) #only two reductions.
# 1.4 find_product(cart, productID): Returns a list of integers, which are indexes of the searched product in the cart.
# E.g. find_product(cart, 12) may return [2,5,10] if the cart contains a the product with productID at index 2,5 and 10.
def find_product(productID):
for i, j in enumerate(cart):
if j == productID:
print(i)
find_product(342453)
# 1.5 get_total(cart): Function that sums up the prices of all products in the cart and returns this value
def get_total():
#Please help me here. :)
get_total()