0
0
Pythonprogramming~20 mins

Why while loop is needed in Python - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
While Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a while loop counting down
What is the output of this Python code?
Python
count = 3
while count > 0:
    print(count)
    count -= 1
print("Done")
A
3
2
1
B
3
2
1
Done
C
1
2
3
Done
DDone
Attempts:
2 left
💡 Hint
Think about how the while loop runs as long as the condition is true.
🧠 Conceptual
intermediate
2:00remaining
Why use a while loop instead of a for loop?
Which situation best shows why a while loop is needed instead of a for loop?
AWhen you want to repeat an action until a condition changes, but you don't know how many times.
BWhen you want to repeat an action a fixed number of times.
CWhen you want to loop through items in a list.
DWhen you want to create a list of numbers.
Attempts:
2 left
💡 Hint
Think about loops that run until something changes, not a set count.
Predict Output
advanced
2:00remaining
Output of a while loop with break
What is the output of this code?
Python
i = 0
while True:
    if i == 3:
        break
    print(i)
    i += 1
print('Stopped')
AStopped
B
0
1
2
C
0
1
2
3
Stopped
D
0
1
2
Stopped
Attempts:
2 left
💡 Hint
Look at when the break stops the loop.
Predict Output
advanced
2:00remaining
What error does this code raise?
What error will this code raise?
Python
count = 5
while count > 0:
    print(count)
    count -= 1
ASyntaxError
BNameError
CIndentationError
DNo error, prints 5 to 1
Attempts:
2 left
💡 Hint
Check the syntax of the while statement.
🧠 Conceptual
expert
3:00remaining
Why is a while loop needed for user input validation?
Why is a while loop the best choice to keep asking a user for input until they enter a valid number?
ABecause the number of attempts is fixed and known in advance.
BBecause while loops are faster than for loops.
CBecause the program needs to repeat until the user enters valid input, which could take any number of tries.
DBecause a for loop can only be used with lists.
Attempts:
2 left
💡 Hint
Think about repeating until a condition is met, not a fixed count.