Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions boolean_algebra/and_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def and_gate(input_1: int, input_2: int) -> int:
>>> and_gate(1, 1)
1
"""
if input_1 not in (0, 1) or input_2 not in (0, 1):
raise ValueError("Inputs must be 0 or 1")
return int(input_1 and input_2)


Expand All @@ -41,10 +43,29 @@ def n_input_and_gate(inputs: list[int]) -> int:
>>> n_input_and_gate([1, 1, 1, 1, 1])
1
"""
if not inputs:
raise ValueError("Input list cannot be empty")
if any(x not in (0, 1) for x in inputs):
raise ValueError("All inputs must be 0 or 1")
return int(all(inputs))


if __name__ == "__main__":
import doctest

doctest.testmod()
print("\n--- N-Input AND Gate Simulator ---")
try:
n = int(input("Enter the number of inputs: "))
inputs = []
for i in range(n):
val = int(input(f"Enter input {i + 1} (0 or 1): "))
if val not in (0, 1):
raise ValueError("Inputs must be 0 or 1")
inputs.append(val)

result = n_input_and_gate(inputs)
print(f"Inputs: {inputs}")
print(f"AND Gate Output: {result}")
except ValueError as e:
print("Error:", e)