← Back to Blog

Nested Loops Mini Project — Password Strength Checker

The problem...

You've built the Password Strength Checker before. It checked one password and gave a verdict.

But a real password checker doesn't stop after one try. It keeps asking until you get it right.

The idea!

while True keeps the program running. for checks every character of the password. When all four conditions are met — break ends the loop.

Your mission

Keep asking for a password until the user provides one that passes all four checks. Show feedback after every attempt.

The solution

while True:
    password = input("Enter a password: ")

    has_upper   = False
    has_lower   = False
    has_digit   = False
    has_special = False

    for char in password:
        if char.isupper():
            has_upper = True
        elif char.islower():
            has_lower = True
        elif char.isdigit():
            has_digit = True
        elif not char.isalpha() and not char.isdigit():
            has_special = True

    print(f"Uppercase:         {'✓' if has_upper else '✗'}")
    print(f"Lowercase:         {'✓' if has_lower else '✗'}")
    print(f"Digit:             {'✓' if has_digit else '✗'}")
    print(f"Special character: {'✓' if has_special else '✗'}")

    if has_upper and has_lower and has_digit and has_special:
        print("Strong password. Access granted.")
        break
    else:
        print("Weak password. Try again.\n")

Test it

# Enter a password: abc123
# Uppercase:         ✗
# Lowercase:         ✓
# Digit:             ✓
# Special character: ✗
# Weak password. Try again.

# Enter a password: P@ssw0rd!
# Uppercase:         ✓
# Lowercase:         ✓
# Digit:             ✓
# Special character: ✓
# Strong password. Access granted.

What's really happening

while True keeps the program alive. Every iteration — the flags reset, the for loop checks every character, feedback is printed. If all four flags are Truebreak ends the while. If not — the loop asks again.

The flags reset inside the while — not inside the for. That's the key detail.

Go further

  • Add a minimum length check — at least 8 characters
  • Count attempts — print "Got it in 3 attempts" at the end
  • Add a maximum attempts limit — lock out after 5 failed tries

What you should understand now

  • while True + for — outer keeps running, inner processes each character
  • Flags reset inside while — fresh check on every attempt
  • break ends the while — only when all conditions are met
  • Nested loops make it possible to process and validate in one clean structure
[ login to bookmark ] // copied! 21 views · 1 min
// resources
Exercise password_strength_checker_2.py
← prev Nested Loops Mini Project — Caesar Cipher next → Nested Loops Mini Project — Hangman
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.