pythone challenge question? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

pythone challenge question?

what does the marked statement mean? a= 1/2 *if 1 & bool(a):

29th Jul 2020, 6:49 AM
Lukas MĆ¼ller
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 šŸ˜ƒšŸ˜ƒ
29th Jul 2020, 8:21 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 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.
29th Jul 2020, 8:29 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
Ipang Your phone's execution is FalsešŸ˜³šŸ˜… bool(0.5) == True I am surprised the else block runs šŸ¤£šŸ¤£
29th Jul 2020, 8:54 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
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.
29th Jul 2020, 7:24 AM
Ipang
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
29th Jul 2020, 8:05 AM
Lukas MĆ¼ller
0
Tomiwa Joseph thanks. so shouldn't it be (and) instead of (&)?
29th Jul 2020, 8:24 AM
Lukas MĆ¼ller
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.
29th Jul 2020, 8:50 AM
Ipang
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.
29th Jul 2020, 9:06 AM
Ipang