runtime
234 / 500
question 234 Debugging & Logging · easy

What's wrong with this product record

Before an import decides whether to fix, skip or stop, it needs to know exactly what is wrong with each Open Food Facts record. Write product_problems(product): return the list of problems found, in a fixed order, or [] for a clean record. Missing keys are possible, and nothing should crash.

Functionproduct_problems(product)
producta dictionary that may have code, product_name and sugars_100g (all text, or None)
Codemissing, None or blank → "missing code"; otherwise not all digits, or not 8 or 13 long → "bad code"
Namemissing, None or blank (spaces only count as blank) → "missing name"
Sugarsmissing, None or blank → "missing sugars"; not a number → "bad sugars"; below 0 or above 100 → "sugars out of range" (0 and 100 are fine)
Ordercode, then name, then sugars; at most one problem for each
Examples
callproduct_problems({'code': '3017620422003', 'product_name': 'Nutella', 'sugars_100g': '56.3'})returns[]
callproduct_problems({'code': '30176', 'product_name': ' ', 'sugars_100g': '-2'})returns['bad code', 'missing name', 'sugars out of range']
callproduct_problems({'code': '', 'product_name': 'Water', 'sugars_100g': None})returns['missing code', 'missing sugars']

Loading your progress…

What's wrong with this product record — Python practice · Runtime