Home > OS >  How would I force user input to be only 1 and 0 in my code
How would I force user input to be only 1 and 0 in my code

Time:11-23

So im trying to force the user to give me purely an input between 1 and 0 and I managed to do so for the most part but it'll only work if all three inputs are above that and my code only gives me and input for a

def AND(a, b):
    return a and b

def OR(a, b):
    return a and b
    
    
def NOR(a, b):
    return a and b
        
    
user=[]
    
def main():
    a= False
    b= False
    c= False
    n_attempts = 1
    for _ in range(n_attempts):
        
        a_raw = input("for a, 1 or 0: ")
        try:
            a = int(a_raw)
        except ValueError:
            print(f"Invalid value for 'a': {a!r}")
            continue
        b_raw = input("for a, 1 or 0: ")
        try:
            b = int(b_raw)
        except ValueError:
            print(f"Invalid value for 'a': {b!r}")
            continue
        c_raw = input("for a, 1 or 0: ")
        try:
            c = int(c_raw)
        except ValueError:
            print(f"Invalid value for 'a': {c!r}")
            continue
    
        
    print ("Result of (A NOR B) OR (B AND C) is: " , int(OR(NOR(a, b), AND(b, c))))
    

    
main()

i tried if and elif statements and also work to some degree where itll activate if all inputs are above 1 or 0

for _ in range(3):
    a=input("for a, 1 or 0: ")
        
    b=input("for b, 1 or 0: ")
        
    c=input("for c, 1 or 0: ")
    if a =="0" or a=="1":
        break
    else:
        print("wrong input")
    if b =="0" or b=="1":
        break
    else:
        print("wrong input")
        
    if c =="0" or c=="1":
        break
    else:
        print("wrong input")

im supposed to writethe code as blocks in functions that will perform each gate. There will be one gate per function. Pass the inputs to the functions and the outputs from the functions.

This is my reference for the gate

using that as a reference

CodePudding user response:

You can use a while loop to keep asking for a valid input until it gets one. Use a for loop to iterate through the names and store input values in a dict to avoid duplicate code:

values = {}
for name in 'a', 'b', 'c':
    while True:
        try:
            value = input(f'for {name}, 1 or 0: ')
            value = values[name] = int(value)
            assert value in (0, 1)
            break
        except (ValueError, AssertionError):
            print(f"Invalid value for '{name}': {value!r}")

print(values['a'], values['b'], values['c'])

Demo: https://replit.com/@blhsing/AcclaimedYawningExpertise

CodePudding user response:

Okay, so I'm assuming a lot here, but I gather that what you want is code that does the thing in the logic gate image. I recommend working backwards from Q.

(I'm using empty parentheses for placeholders.)

So, for the first one back from Q is or. So ()or(). A function for that would be

def or_function(a, b):
    return a or b

Using that function would make it or_function((), ()).

Then, it's A nor B, or not (A or B) on the top. (not (A or B)) or () Similarly, a function for that would be

def nor_function(a, b):
    return not(a or b)

Using functions, it would now be or_function(nor_function(A, B), ()).

Then, it's B and C on the bottom. The final answer is (not (A or B)) or (B and C).

Since this seems to be your homework, I'll leave you to the last one - it should be fairly similar to the others.

Note: If input is 0 or 1, then you need to convert 0 to False and 1 to True.

CodePudding user response:

you should not direct assignment the value of input to a or b or c

value = input("some description here")
if value in ["0", "1"]:
    a = int(value)
else:
    break

CodePudding user response:

I don't know if this is what you want to do.

a=bool
b=bool
c=bool

while True:

    v=["1","0"]
    a=input("input 1 or 0: ")
    b=input("input 1 or 0: ")
    c=input("input 1 or 0: ")
    if (a and b and c in v):
        break
    else:
        print("wrong input")
  • Related