Learn Python in 30 Days — Day 9: While Loops
Learn Python in 30 Days — Day 9: While Loops
Welcome to Day 9 of the Learn Python in 30 Days series!
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
Yesterday, we explored for loops which is perfect for repeating actions over known sequences.
Today, we’re tackling while loops, which keep running until a condition changes.
By the end of this lesson, you’ll understand:
-
How while loops work
-
How to avoid infinite loops
-
When to use them vs. for loops
-
How to build a Password Retry mini-project
Step 1 – What Is a While Loop?
A while loop runs as long as its condition remains True.
How it works:
-
The loop checks the condition
count < 5. -
If it’s true, it runs the indented code.
-
Each time, it adds 1 to
count. -
When
countreaches 5, the condition becomes false and the loop stops.
Step 2 – Watch Out for Infinite Loops
If your condition never becomes False, your program will loop forever.
Example of a bug:
Press Ctrl + C to stop a looping script like this.
Always make sure your loop changes something inside it.
Try typing this into the IDLE IDE and running it and you should see the following output. Warning this can crash due to the infinite loop!
You can also download this example from my GitHub here and run it yourself.
Step 3 – Using While for User Input
while loops are great for keeping the program running until the user does something specific.
Example: ask for a number until it’s correct.
The loop keeps going until the user enters 7.
Try typing this into the IDLE IDE and running it and you should see the following output.
You can also download this example from my GitHub here and run it yourself.
Step 4 – Mini Project: Password Retry
Let’s make a small project that lets a user retry a password up to three times.
How it works:
-
The loop runs while
attempts < 3. -
If the user gets it right, the loop stops using
break. -
If not, the attempt count increases.
-
After 3 tries, the program ends with a denial message.
Step 5 – When to Use While Loops
Use a while loop when:
-
You don’t know how many times you’ll need to loop.
-
The repetition depends on something changing inside the loop (like user input or a sensor value).
Use a for loop when:
-
You already know how many items or iterations you’ll loop through.
Challenge for Today
Create your own password system that:
-
Hides the password text (use
getpassfrom thegetpassmodule). -
Locks out the user for 10 seconds after 3 failed attempts (hint:
time.sleep(10)).
Please note that this will only work in the command line prompt and not in IDLE.
You can also download to see the challenge code from my GitHub here and run it yourself.
Next up — Day 10 – Lists
You’ll learn how to make lists and how to create, index, slice, append, remove items from them.All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!
Hope you have enjoyed this post, thanks Matty



Comments
Post a Comment