+ 1
A wrong challenge?
Is there wrong challenge questions? I answered 1 for the quesiton and it shows wrong, then i choosed 0 for the answer it shows correct in the second time. I was confused and i runned the code into Python IDLE, the result is definitly 1. The code: a = [1, 2**2] b = [1, 2*2] x = 2*2 y = 8/2 if (x is y)|(a is b): print(1) else: print(0) Is there other wrong challenge questions like this, please share your discoveries.
9 Answers
+ 6
I don't know Python but it's output is 0 in Code Playground ^_^
+ 5
x is y = False
a is b = False
False|False = False
So 0.
+ 5
Sapphire, seem like the author's intent was just that, to check object equality, not value equality, wouldn't be much of a challenge otherwise ;D
+ 2
me neitherïŒ I runned this in python Idle, the result shows 1, hope someone can explain to me.
+ 2
Python Programmer here to help o/
So your answer is actually correct, it's actually the fault of the code itself. Since the author used 'is' instead of '==' to represent equality.
'Is' checks to see if the two 'object's are the same in system memory, immutable ones.
== checks for the value
'Is' can work on small mutable values, but should not be depended upon. This would be a different case if it were comparing strings since they're immutable.
If you want confirm this yourself, you can do this by changing 'is' to '=='
So unless the test was to spot this, I don't see your assumption of it being 1 wrong.
+ 1
In python 3 the value of 8/2 with be an float pointer number, so x != y.
The is operator on arrays will compare their addresses so a != b.
Both conditions are false so the if is false.
+ 1
thanks Sapphire, thanks everyone!
+ 1
thank you everyone, I find that the code is right, I reinstalled the python and now it shows 0, but my sublime text 3 still keep showing 1.
0
@Maya
It's not a very well known to a lot of programmers. Totally unfair boss fight challenge. Nerf bat needed