Editor

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 lesson

Use 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 accepted
password = "hello5world"
# Check if password contains at least one digit using for-else

More break, continue, pass exercises

All Python exercises · 50 practice problems · Control Flow quiz