QUESTION IMAGE
Question
7 what will the follo ng program print when run?
for i in range(2):
x = 3
while x >= 1:
print (x)
x = x - 1
a. 3
2
1
0
3
2
1
0
b. 3
2
1
3
2
1
c. 1
2
3
1
2
3
d. 0
1
2
3
Step1: Analyze the for loop
The for i in range(2) loop runs 2 times.
Step2: Analyze the while loop in each iteration
- In each iteration of the
forloop,xis set to 3. - Then the
while x >= 1loop runs: - First iteration of
while:x = 3, print 3, thenx=x - 1=2. - Second iteration of
while:x = 2, print 2, thenx=x - 1=1. - Third iteration of
while:x = 1, print 1, thenx=x - 1=0. - Now
x = 0which makes thewhileloop conditionFalseand exits thewhileloop. - Since the
forloop runs 2 times, the sequence3 2 1is printed twice.
So the output is 3, 2, 1, 3, 2, 1 which corresponds to option B.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
B. 3
2
1
3
2
1