Compito
Aggiornare i livelli di stock dei prodotti in base alle vendite e alle spedizioni utilizzando i cicli.
List of products with their initial stock levels at the start of the week
products = [
["Apples", 150],
["Bananas", 200],
["Oranges", 100],
["Mangoes", 120]
]
List of products sold by the end of the week
units_sold = [["Apples", 30], ["Bananas", 45], ["Oranges", 20], ["Mangoes", 10]]
New shipment received at the end of the week
shipment_received = [["Apples", 50], ["Bananas", 70], ["Oranges", 30], ["Mangoes", 40]]
Utilizzare un ciclo for con iterazione tramite indice per scorrere la lista products.
for i in range(len(products)):
Per ogni prodotto, sottrarre il numero di unità vendute (units_sold) dallo stock di products.
product_name = products[i][0]
for sold in units_sold:
if sold[0] == product_name:
products[i][1] -= sold[1]
break
Utilizzare un secondo ciclo for (anch'esso con iterazione tramite indice) per scorrere nuovamente products.
for i in range(len(products)):
product_name = products[i][0]
for shipment in shipment_received:
if shipment[0] == product_name:
products[i][1] += shipment[1]
break
Aggiungere il valore corrispondente da shipment_received per aggiornare lo stock.
Alla fine, stampare: Final stock levels for all products:
print("Final stock levels for all products:", products)