0

I am having an issue with Python validation. I was wondering if there was a simple way to put validation on two input numbers to check a few things:
If the inputs are ints
if neither of the inputs equals a certain number, they are not valid. (For example, this would mean one of them would have to be 5. So a = 1 b = 4, a = 3 b = 2, a = 1 b = 1 wouldn't work)
If the two numbers are the same number that is required it will not work (E.G. if a = 5 b = 5 will not work as 5 is the required number, however a = 1 b = 5 would work as 5 is only being inputted once).

while True:
    a = input("Enter first input: ")
    b = input("Enter second input: ")
    try:
        val = int(a)
        val1 = int(a)
        if val1 != 5 or val != 5:
            print("I'm sorry but it must be a pos int and equal 5")
            continue
        break
    except ValueError:
        print("That's not an int")

This is what I was trying to do, but I think I may be dreadfully wrong?
Any help appreciated!

Thanks.

Cole
  • 1,715
  • 13
  • 23

3 Answers3

2

Logical xor

You should continue the loop if exactly one a and b is equal to 5. It means you need a logical xor. Parens are needed to avoid comparing a to 5 ^ b:

while True:
    a = input("Enter first input: ")
    b = input("Enter second input: ")
    try:
        a = int(a)
        b = int(b)
        if (a != 5) ^ (b != 5):
            print("I'm sorry but it must be a pos int and equal 5")
            continue
        break
    except ValueError:
        print("That's not an int")

It might not be very readable though.

Count 5's

You could count the number of ints equal to 5:

while True:
    a = input("Enter first input: ")
    b = input("Enter second input: ")
    try:
        a = int(a)
        b = int(b)
        count5 = [a, b].count(5)
        if count5 == 1:
            break
        else:
            print("Exactly one input should be equal to 5.")
    except ValueError:
        print("That's not an int")

If you want to differentiate between errors:

while True:
    a = input("Enter first input: ")
    b = input("Enter second input: ")
    try:
        a = int(a)
        b = int(b)
        count5 = [a, b].count(5)
        if count5 == 2:
            print("Inputs cannot both be equal to 5.")
        elif count5 == 0:
            print("At least one input should be equal to 5.")
        else:
            break
    except ValueError:
        print("That's not an int")

Here's an example:

Enter first input: 3
Enter second input: 1
At least one input should be equal to 5.
Enter first input: 5
Enter second input: 5
Inputs cannot both be equal to 5.
Enter first input: 3
Enter second input: 5
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
0

Logic... if True then break, False then print error message...

Two things... you want logical exclusive or so that True ^ True = False You aren't storing b. The text you're printing doesn't explain what's happening.

  while True:
    a = input("Enter first input: ")
    b = input("Enter second input: ")
    try:
        a = int(a)  # like the readable code using a not val1... 
        b = int(b)
        if (a != 5) ^ (b != 5):  # added parens as suggested
            break
        else:
            print("I'm sorry but it must be a pos int and equal 5")
            continue
    except ValueError:
        print("That's not an int")

Output

Enter first input: 5

Enter second input: 5
I'm sorry but it must be a pos int and equal 5

Enter first input: 1

Enter second input: 5

runfile('...')

Enter first input: 5

Enter second input: 5
I'm sorry but it must be a pos int and equal 5
Blue Shrapnel
  • 95
  • 1
  • 13
0

You could use:

(a == 5 and b != 5) or (a != 5 and b == 5)

Or this:

(a == 5) != (b == 5)

(!= is the boolean equivalent of bitwise xor)

Francisco
  • 10,918
  • 6
  • 34
  • 45