Essence: there is a dictionary where each element (product) is assigned a price Task: calculate the total cost of products that are on the list Problem: if you add 2 identical elements from the dictionary to the list (for example ["pomme", "orange", "orange" ], then identical elements are counted 1 time, not 2 (for example, for ["pomme", "orange", "orange"] should return the total amount of 5, and return 3.5). How then to prescribe the condition?

prix = { "banane": 4, "pomme": 2, "orange": 1.5, "poire": 3 } def calculer_facture(nourriture): total = 0 for produit in prix: if produit in nourriture: total = total + prix[produit] return total 

    2 answers 2

    Something like this:

     >>> prix = {"banane": 4, "pomme": 2, "orange": 1.5, "poire": 3} >>> nourriture = ["pomme", "orange", "orange"] >>> sum(prix[n] for n in nourriture) 5.0 
       prix = {"banane": 4, "pomme": 2, "orange": 1.5, "poire": 3} def calculer_facture(nourriture): total = 0 for produit in prix: if produit in nourriture: total += prix[produit] * nourriture.count(produit) return total