Password Checker with for-else
Beginner · Write the code · from Python break, continue, pass: Controlling Loop Flow
From the lesson: Python break, continue, pass: Controlling Loop Flow
You're searching through a phone book for your friend's number. Once you find it, do you keep flipping through every remaining page? Of course not - you stop. Or imagine you're reading a list of emails. You skip the spam and only read the real ones. These are everyday examples of controlling the flow of a loop.
Read the lessonUse a for-else pattern to check if a password contains at least one digit. Loop through each character in the password. If you find a digit, print "Password accepted" and break. If the loop finishes without finding a digit, print "Password must contain a number".
Expected output for the given password:
Password acceptedMore break, continue, pass exercises
- Find the First Negative NumberBeginner
- Print Without VowelsBeginner
- Predict the OutputBeginner
- Fix the Bug: Sum Until NegativeBeginner
All Python exercises · 50 practice problems · Control Flow quiz