Conor Hastings Conor Hastings - 1 month ago 9
Python Question

getting a python while statement using != to work

So i have a program called lonesum(a,b,c) that needs to add the 3 numbers entered together only if they are input once. so lonesum(1,2,3) would return 6 but lonesum(1,1,3) would return 3. I have that part. What i need help with is getting a statement to work that will return an error if the user enters anything that isn't an integer i.e. lonesum(.5,2,3) will return error. so far i have this while statement which i thought would work but doesn't,:

while (a,b,c) != int:
print("Error")
return None
while a==b==c:
return 0
while a==b:
return c
while b==c:
return a
while a==c:
return b
while a!=b!=c:
sum1=(a+b+c)
return sum1


(That is the rest of my code, that is the part that works)

My problem is the != doesn't work and I'm not sure what to do. Any suggestions?

Answer

Try something like this:

if not all(type(v) is int for v in (a, b, c)):
    print("Error!")
    return None

What you want to check is if each value in (a, b, c) is of integer type. So you must check each value, not the tuple itself.

The builtin function all iterates over something iterable and returns true if all of the values are true. I've created a generator expression that iterates over the tuple (a, b, c), comparing the type of each value to int. all iterates over that generator expression. So if all of the values in (a, b, c) have type int, it returns True.