def makes10(a, b):
return ((a or b) is 10) or (a+b is 10)
makes10(9, 10) False
I hope the above is same as the below, but returning different results.
def makes10(a, b):
return (a == 10 or b == 10 or a+b == 10)
makes10(9, 10) True
def makes10(a, b):
return ((a or b) is 10) or (a+b is 10)
makes10(9, 10) False
I hope the above is same as the below, but returning different results.
def makes10(a, b):
return (a == 10 or b == 10 or a+b == 10)
makes10(9, 10) True
>>> (1 or 10) is 10
False
>>> (10 or 1) is 10
True
>>> (1 or 10)
1
using or and is to check if either of those numbers is 10 just doesn't work...
the bottom version of makes10() is probably the way to go. as @Wooble said, don't use is to compare integer values.
Python will take a and b as 2 different objects.
Using "is" compares the objects, while "==" will do comparison by value.
For example if you have a=10,
a==10 should return true and a is 10 should return false