{"record":{"id":"6f4cf5660f69a5e9","repo":"TheAlgorithms/Python","slug":"invalid-weight-of-weight-f-provided","errorCode":null,"errorMessage":"Invalid weight of {weight:f} provided","messagePattern":"Invalid weight of (.+?) provided","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"other/scoring_algorithm.py","lineNumber":73,"sourceCode":"        # for weight 0 score is 1 - actual score\n        if weight == 0:\n            for item in dlist:\n                try:\n                    score.append(1 - ((item - mind) / (maxd - mind)))\n                except ZeroDivisionError:\n                    score.append(1)\n\n        elif weight == 1:\n            for item in dlist:\n                try:\n                    score.append((item - mind) / (maxd - mind))\n                except ZeroDivisionError:\n                    score.append(0)\n\n        # weight not 0 or 1\n        else:\n            msg = f\"Invalid weight of {weight:f} provided\"\n            raise ValueError(msg)\n\n        score_lists.append(score)\n\n    return score_lists\n\n\ndef generate_final_scores(score_lists: list[list[float]]) -> list[float]:\n    \"\"\"\n    >>> generate_final_scores([[1.0, 0.0, 0.33333333333333337],\n    ...                        [0.75, 0.0, 1.0],\n    ...                        [0.25, 1.0, 0.0]])\n    [2.0, 1.0, 1.3333333333333335]\n    \"\"\"\n    # initialize final scores\n    final_scores: list[float] = [0 for i in range(len(score_lists[0]))]\n\n    for slist in score_lists:\n        for j, ele in enumerate(slist):","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/other/scoring_algorithm.py#L55-L91","documentation":"Raised by the scoring loop in scoring_algorithm when the weight argument for a criterion is neither 0 nor 1. Weight 0 produces rank-based scores, weight 1 produces min-max normalized scores, and there is no interpolation between them, so any other value is invalid.","triggerScenarios":"Calling the scoring function with weights like 0.5, 2, or -1 for any criterion in the weights list — e.g. weights=[0, 0.5, 1] fails on the second element.","commonSituations":"Assuming weights are continuous mixing coefficients (0.0-1.0) as in weighted-sum scoring models, or loading weights from config where an intermediate value was entered.","solutions":["Use only 0 (rank-based) or 1 (min-max) for each criterion's weight","If you need custom mixing, compute both score lists with weights 0 and 1 and blend them yourself afterwards","Validate the weights list up front: assert all(w in (0, 1) for w in weights)"],"exampleFix":"# before\nscore_lists = calculate_scores(..., weights=[0, 0.5, 1])  # ValueError on 0.5\n\n# after\nrank_scores = calculate_scores(..., weights=[0, 0, 0])\nminmax_scores = calculate_scores(..., weights=[1, 1, 1])\n# blend rank/min-max per criterion yourself if intermediate behavior is needed","handlingStrategy":"validation","validationCode":"def valid_weights(weights) -> bool:\n    return all(w in (0, 1) for w in weights)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Document weights as mode selectors (0 = rank, 1 = min-max), not continuous coefficients","Validate weight arrays against {0, 1} before invoking the scorer"],"tags":["scoring","argument-validation","weights"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}