Computer Science
Predict the output of the following code fragments:
x = [1,2,3]
counter = 0
while counter < len(x):
print(x[counter] * '%')
for y in x:
print(y * '* ')
counter += 1
Answer
%
*
* *
* * *
%%
*
* *
* * *
%%%
*
* *
* * *
Working
In this code, the for loop is nested inside the while loop. Outer while loop runs 3 times and prints % as per the elements in x in each iteration. For each iteration of while loop, the inner for loop executes 3 times printing * as per the elements in x.
Related Questions
Predict the output of the following code fragments:
x = 10 y = 5 for i in range(x-y * 2): print (" % ", i)
Predict the output of the following code fragments:
c = 0 for x in range(10): for y in range(5): c += 1 print (c)
Predict the output of the following code fragments:
for x in 'lamp': print(str.upper(x))
Predict the output of the following code fragments:
x = 'one' y = 'two' counter = 0 while counter < len(x): print(x[counter], y[counter]) counter += 1