From ae11088a7a775356c605004c64d47b8dc11a3e06 Mon Sep 17 00:00:00 2001 From: Henry-the-Sun Date: Wed, 22 Oct 2025 20:51:03 -0700 Subject: [PATCH 1/2] Update and_gate.py Add Input Validation and Interactive Mode to N-Input AND Gate. This pull request adds input validation and an interactive mode to the N-input AND gate program. It ensures all inputs are limited to binary values (0 or 1) and provides clear error messages for invalid input. Additionally, users can now interactively enter any number of inputs to see the AND gate output calculated in real time, making the program more robust and user-friendly. --- boolean_algebra/and_gate.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/boolean_algebra/and_gate.py b/boolean_algebra/and_gate.py index 650017b7ae10..d579ced5c06d 100644 --- a/boolean_algebra/and_gate.py +++ b/boolean_algebra/and_gate.py @@ -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) @@ -41,10 +43,30 @@ 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) From 536ceb0709baceb48db274b193d10bad8b9761ef Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 23 Oct 2025 03:57:06 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boolean_algebra/and_gate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/boolean_algebra/and_gate.py b/boolean_algebra/and_gate.py index d579ced5c06d..15ce8482d574 100644 --- a/boolean_algebra/and_gate.py +++ b/boolean_algebra/and_gate.py @@ -50,7 +50,6 @@ def n_input_and_gate(inputs: list[int]) -> int: return int(all(inputs)) - if __name__ == "__main__": import doctest