Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why while loop is needed
📖 Scenario: Imagine you want to keep asking a friend if they want to play a game until they say yes. You don't know how many times you will need to ask. This is like a real-life situation where you repeat something until a condition is met.
🎯 Goal: You will build a simple program that keeps asking the user to enter the word yes. The program will stop only when the user types exactly yes. This shows why a while loop is useful for repeating actions until a condition is true.
📋 What You'll Learn
Create a variable called answer and set it to an empty string ""
Create a while loop that runs as long as answer is not equal to "yes"
Inside the loop, ask the user to type "yes" using input() and store it in answer
After the loop ends, print "Thank you! You typed yes."
💡 Why This Matters
🌍 Real World
While loops are used in many programs to keep doing something until a condition is met, like waiting for user input, checking sensor data, or repeating tasks.
💼 Career
Understanding while loops is important for programming jobs because many real-world problems need repeated actions that stop only when certain conditions happen.
Progress0 / 4 steps
1
Set up the initial variable
Create a variable called answer and set it to an empty string "".
Python
Hint
Think of answer as a box that starts empty and will hold what the user types.
2
Create the while loop condition
Create a while loop that runs as long as answer is not equal to "yes".
Python
Hint
The loop should keep going until the user types exactly "yes".
3
Ask the user inside the loop
Inside the while loop, ask the user to type "yes" using input() and store it in answer.
Python
Hint
Use input() to get text from the user and save it in answer.
4
Print a thank you message after the loop
After the while loop ends, print "Thank you! You typed yes.".
Python
Hint
Use print() to show the message after the user types "yes".
Practice
(1/5)
1. Why do we use a while loop in Python instead of a for loop?
easy
A. Because for loops cannot repeat code.
B. Because we don't always know how many times the loop should run before starting.
C. Because while loops are faster than for loops.
D. Because while loops do not need a condition.
Solution
Step 1: Understand the difference between while and for loops
for loops run a fixed number of times, while while loops run as long as a condition is true.
Step 2: Identify when while loops are needed
while loops are useful when the number of repetitions is unknown before starting.
Final Answer:
Because we don't always know how many times the loop should run before starting. -> Option B
Quick Check:
Unknown repetitions = use while loop [OK]
Hint: Use while when repeat count is unknown before start [OK]
Common Mistakes:
Thinking for loops can handle unknown repetitions
Believing while loops don't need conditions
Confusing speed differences between loops
2. Which of the following is the correct syntax to start a while loop in Python?
easy
A. while x > 0: print(x)
B. while x > 0 print(x):
C. while (x > 0) print x
D. while x > 0 { print(x) }
Solution
Step 1: Recall Python's while loop syntax
Python requires a colon (:) after the condition and indentation for the loop body.
Step 2: Check each option
while x > 0: print(x) uses colon and correct indentation style (single line allowed). Others have syntax errors.
Final Answer:
while x > 0: print(x) -> Option A
Quick Check:
Colon after condition = correct syntax [OK]
Hint: Remember colon (:) after condition in while loops [OK]
We want to keep asking until the user types 'secret'. This requires input inside the loop and proper exit condition.
Step 2: Analyze each option
password = ''\nwhile password != 'secret':\n password = input('Enter password: ')
print('Access granted') initializes empty password, checks condition, inputs inside loop, updates, and exits when equal to 'secret'. while true:\n password = input('Enter password: ')
if password == 'secret':\n break
print('Access granted') uses 'true' which is undefined in Python (must be 'True'), causing NameError. for password in ['secret']:\n input('Enter password: ')
print('Access granted') uses for loop incorrectly, runs fixed once without checking input. password = input('Enter password: ')
while password == 'secret':
print('Access granted') inputs once then loops only if correct, fails to re-ask on wrong.