Robotics & Artificial Intelligence
Predict the output of the following snippet:
p = -99
while(p < 0):
p = p + 9
print(p, end=' ')
Python Control Flow
1 Like
Answer
Output
-90 -81 -72 -63 -54 -45 -36 -27 -18 -9 0
Explanation
Let’s break down the code line by line:
1. p = -99: The variable p is assigned the value -99.
2. while(p < 0):: The while loop executes as long as the value of p is less than 0.
3. In each iteration, the statement p = p + 9 increases the value of p by 9 and print(p, end=' ') displays the updated value in the same line separated by spaces.
4. The values of p become -90, -81, -72, -63, -54, -45, -36, -27, -18, -9 and 0.
5. When p becomes 0, the condition p < 0 becomes False and the loop terminates.
6. Therefore, the output is -90 -81 -72 -63 -54 -45 -36 -27 -18 -9 0.
Answered By
1 Like
Related Questions
Predict the output of the following snippet:
k = 1 i = 2 while(i < 6): k = k * i i = i + 1 print(k)Predict the output of the following snippet:
p = 100 for i in range(1, 5): p = p - 1 print(p, end=' ')Predict the output of the following snippet:
for i in range(4): for j in range(i, -1, -1): print(j, end=' ') print()Predict the output of the following snippet:
for x in range(1, 4): for y in range(1, 3): print(x*y, end=' ') print()