Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis0
pythone challenge question?
what does the marked statement mean? a= 1/2 *if 1 & bool(a):
8 Respostas
+ 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.