TheAlgorithms/Python · info · ValueError

Input is invalid

Error message

Input is invalid

What it means

Raised at the trailing else of calculate_resistance() in electronics/resistor_color_code.py when check_validity() returns a falsy value. In the current code this branch is effectively dead: check_validity() either returns True or raises its own specific ValueError (invalid band count, wrong color count, unknown color), so in practice every invalid input surfaces one of those more specific messages instead. If you ever see 'Input is invalid', it means the validity contract changed (e.g. check_validity returning False) — check your installed version.

Source

Thrown at electronics/resistor_color_code.py:368

        if number_of_bands != 6:
            temperature_coeffecient_color = None
        else:
            temperature_coeffecient_color = color_code_list[
                number_of_significant_bands + 2
            ]
        temperature_coeffecient = (
            0
            if temperature_coeffecient_color is None
            else get_temperature_coeffecient(str(temperature_coeffecient_color))
        )
        resisitance = significant_digits * multiplier
        if temperature_coeffecient == 0:
            answer = f"{resisitance}Ω ±{tolerance}% "
        else:
            answer = f"{resisitance}Ω ±{tolerance}% {temperature_coeffecient} ppm/K"
        return {"resistance": answer}
    else:
        raise ValueError("Input is invalid")


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Don't chase this message directly — the specific cause is reported earlier by check_validity/get_band_type_count; read the full traceback.
  2. If you maintain a fork, make check_validity always raise with a specific message (or always return True) so this fallback stays dead.
  3. Verify you are importing the intended module file (check module.__file__) and clear stale bytecode.
Defensive patterns

Strategy: try-catch

Validate before calling

ok = check_validity(number_of_bands, color_code_list)  # raises the specific error first
result = calculate_resistance(number_of_bands, color_code_list)

Try / catch

try:
    result = calculate_resistance(number_of_bands, color_code_list)
except ValueError as exc:
    # read the message: specific causes come from check_validity, not this branch
    handle_invalid_input(str(exc))

Prevention

When it happens

Trigger: Practically unreachable with the current check_validity implementation; reachable if a modified/older version of check_validity returns None/False instead of raising, or if a subclass or monkeypatch alters its return value.

Common situations: Vendored or forked copies of the file where check_validity was changed to return False; version drift between the module you read and the one imported (stale __pycache__ or a different package on sys.path).

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/f237c5ac5b69ae13. Report an issue: GitHub.