Why the answer is 0? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Why the answer is 0?

a=2 b=0 while(a or b): a-=1 print(a) and Why when b is not 0 Time limit exceeded occurred ?

11th Mar 2017, 7:27 AM
Geovim
Geovim - avatar
2 Answers
+ 2
value of a is 0 because print is outside of while loop and it will print final value of a which is 0. in first loop: a=2-1=1 second loop: a=1-1=0 now both a and b are 0 so loop will terminate.
11th Mar 2017, 8:23 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
+ 2
In python a variable is false when it's equal to 0, so the while loop will run until both a and b are equal to 0. Therefore a will be equal to 0 in the end as it's decremented in the loop.
11th Mar 2017, 5:51 PM
Karol Musolff
Karol Musolff - avatar