TheAlgorithms/Python · error · ValueError
Inputs and select signal must be 0 or 1
Error message
Inputs and select signal must be 0 or 1
What it means
Raised by mux() when any of input0, input1, or select is not exactly the int 0 or 1. The multiplexer models a hardware 2-to-1 MUX, so all three signals must be strict binary; the check uses membership `i in (0, 1)` which also rejects 1.0, True-adjacent floats, and any other value.
Source
Thrown at boolean_algebra/multiplexer.py:36
1
>>> mux(1, 0, 1)
0
>>> mux(2, 1, 0)
Traceback (most recent call last):
...
ValueError: Inputs and select signal must be 0 or 1
>>> mux(0, -1, 0)
Traceback (most recent call last):
...
ValueError: Inputs and select signal must be 0 or 1
>>> mux(0, 1, 1.1)
Traceback (most recent call last):
...
ValueError: Inputs and select signal must be 0 or 1
"""
if all(i in (0, 1) for i in (input0, input1, select)):
return input1 if select else input0
raise ValueError("Inputs and select signal must be 0 or 1")
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Normalize signals to binary before calling: s = int(bool(s)).
- Validate all three arguments against (0, 1) in your caller and reject/clip earlier.
- If using -1/1 encoding, map to 0/1 first: (x + 1) // 2.
Example fix
# before mux(0, 1, 0.7) # ValueError: Inputs and select signal must be 0 or 1 # after mux(0, 1, int(select > 0.5)) # binary select
Defensive patterns
Strategy: validation
Validate before calling
if not all(i in (0, 1) for i in (input0, input1, select)):
raise ValueError('mux signals must be binary 0/1') Type guard
def is_binary(v: object) -> bool:
return isinstance(v, int) and v in (0, 1) Prevention
- Normalize booleans with int(bool(x)) before hardware-style gates.
- Map -1/1 encodings to 0/1 explicitly.
When it happens
Trigger: Calling mux(0, 2, 1), mux(0, -1, 0), or mux(0, 1, 1.1) as in the doctests. Also mux(True, 1, 0) passes (bool == int 1) but mux(0, 1, 0.0) fails because 0.0 in (0, 1) is True — careful: 0.0 == 0 so floats 0.0/1.0 actually pass; values like 2, -1, 1.1 fail.
Common situations: Feeding unnormalized boolean data (e.g. -1/1 encoding, probabilities, or numpy ints that are fine but floats like 1.5 that are not), or passing results of arithmetic that can exceed 1.
Related errors
- Input list must contain at least two elements
- number must be positive
- The value of input must be non-negative
- plain must contain only lowercase letters (a-z)
- plaintext is empty
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/6be8c70ae8f44342.
Report an issue: GitHub.