Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Password Validator

Validate practice passwords with character rules. Students compare a strong-rule conjunction with a looser medium branch and inspect allowed special symbols.

BeginnerPhase 6 - Category 5: Real-World Logic ProblemsExample 13 of 15
password-validator.py
1# Take password
2password = input("Enter password: ")
3
4# Validate
5has_upper = any(c.isupper() for c in password)
6has_lower = any(c.islower() for c in password)
7has_digit = any(c.isdigit() for c in password)
8has_special = any(c in "!@#$%^&*" for c in password)
9length_ok = len(password) >= 8
10
11if length_ok and has_upper and has_lower and has_digit and has_special:
12 print("Strong password")
13elif length_ok and (has_upper or has_lower) and has_digit:
14 print("Medium password")
15else:
16 print("Weak password")

Output

Enter password: MyPass123!
Strong password

In this one

  • Combine explicit requirements
  • Distinguish strong and medium policies
  • Restrict the special-symbol set

How it works

  1. 1

    Calculate case, digit, symbol, and length booleans.

  2. 2

    Test the full strong conjunction first.

  3. 3

    Otherwise test the medium rule before choosing Weak.

Edge cases

Does a space satisfy has_special?

Does a space satisfy has_special?

No. It is not in the explicit symbol string.

Can a password without a digit be Medium?

Can a password without a digit be Medium?

No. The medium branch requires a digit.

What's going on

Strong requires eight characters, both letter cases, a digit, and a symbol from !@#$%^&*. Medium requires length, at least one letter case, and a digit.

Unlike the five-check scoring lesson, this is a branch policy rather than counting passed booleans. Spaces do not satisfy the listed-symbol rule, though they count toward length. The labels are classroom feedback, not a security guarantee or password-storage method.

Does a space satisfy has_special? No. It is not in the explicit symbol string.

Can a password without a digit be Medium? No. The medium branch requires a digit.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What distinguishes the strong branch?

Pick an answer to check it.

Question 2

Does a space satisfy has_special?

Pick an answer to check it.

Questions people ask

Does a space satisfy has_special?

No. It is not in the explicit symbol string.

Can a password without a digit be Medium?

No. The medium branch requires a digit.

What distinguishes the strong branch?

Every listed requirement must pass.