import json data = open(0).read().strip() part_2 = False def addup(d): r = 0 if isinstance(d, list): for e in d: v = addup(e) if v is not None: r += v elif isinstance(d, dict): for e in d.values(): v = addup(e) if v is None: return 0 r += v elif isinstance(d, str): if part_2 and d == "red": return None else: r += d return r data = json.loads(data) print(addup(data))