pythone challenge question? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 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 😃😃
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