0
pythone challenge question?
what does the marked statement mean? a= 1/2 *if 1 & bool(a):
8 Answers
+ 1
Lukas MĂŒller
If 1 and bool(a)
Explanation:
Bool(any number) is True as long as any number is not 0.
So, bool (a) == True.
1 == True
Now, remember thy truthy/falsey tableđ,
True and True == True
So, if True: print(int(a + 1/2)
Output: 1
Hope this clarifies some things đđ
+ 2
Lukas MĂŒller
"&" and "and" both work in this context but...I use and to avoid confusion. & is used with set() and bitwise operation and stuff.
+ 1
Ipang
Your phone's execution is Falseđłđ
bool(0.5) == True
I am surprised the else block runs đ€Łđ€Ł
0
Lukas MĂŒller
Is that the whole code?
Also it's rather confusing when an expression involves an integer (1) and boolean (1 / 2) as operands while the operator is a bitwise AND.
0
Ipang
the hole code is like this:
a = 1/2
if 1 & bool(a):
print(int(a + 0.5))
else:
print(int(a + 1.5))
and the answer is 1
0
Tomiwa Joseph
thanks.
so shouldn't it be (and) instead of (&)?
0
Lukas MĂŒller
I get different output when running that code in Code Playground and Python editor in my phone. In SoloLearn, the true block is executed, in my phone the else block is the one being executed. I'm not sure which one is valid, actually I'm confused.
0
Tomiwa Joseph
Maybe, but that's the way I see it. Maybe different ways of rounding, causing bool() to interpret 1 / 2 differently. But I really am not sure.