runtime
161 / 500
question 161 Nested Data Structures · easy

Sugar in one serving

Open Food Facts stores nutrition per 100 g inside each product's nutriments dictionary, and leaves out what it doesn't know. A tracking app wants the sugar in the amount someone actually ate.

Functionsugar_in_serving(product, grams)
producta dictionary that may have "nutriments", which may have "sugars_100g" (a number, or None)
Returnssugars_100g * grams / 100, or None when the sugar value is missing or None
Rulesa sugar value of 0 is a real value (0.0 g), not missing
Examples
callsugar_in_serving({'code': '5010029000023', 'brands': ['Weetabix'], 'nutriments': {'energy-kcal_100g': 362, 'sugars_100g': 4.4, 'fiber_100g': 10}, 'allergens_tags': ['en:gluten']}, 40)returns1.76
callsugar_in_serving({'code': '0070470003023', 'brands': ['Yoplait'], 'nutriments': {'sugars_100g': 11.5}, 'allergens_tags': ['en:milk']}, 150)returns17.25
callsugar_in_serving({'code': '0028400090896', 'brands': ["Lay's"], 'nutriments': {}}, 30)returnsNone

Loading your progress…

Sugar in one serving — Python practice · Runtime