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

Basic Password Validation

Validate a practice password with length and digit rules in Python. Students use len, any, and independent feedback checks without confusing this with security.

IntermediatePhase 1 - Level 4: Logical Operators & Compound StatementsExample 10 of 10
basic-password-validation.py
1# Take password
2password = input("Enter password: ")
3
4# Check rules
5has_length = len(password) >= 8
6has_digit = any(char.isdigit() for char in password)
7
8if has_length and has_digit:
9 print("Password is valid")
10else:
11 print("Password is invalid")
12 if not has_length:
13 print("- Must be at least 8 characters")
14 if not has_digit:
15 print("- Must contain at least one digit")

Output

Enter password: password123
Password is valid

Enter password: pass
Password is invalid
- Must be at least 8 characters
- Must contain at least one digit

In this one

  • Build named validation results
  • Use any for existence
  • Report multiple failures

How it works

  1. 1

    Read the password and calculate both booleans.

  2. 2

    Accept only if length and digit checks pass.

  3. 3

    Otherwise print each failed rule independently.

Edge cases

Can both error messages appear?

Can both error messages appear?

Yes. A short password without a digit fails both independent checks.

Does eight spaces and a digit pass?

Does eight spaces and a digit pass?

Yes. The code has no restriction on spaces or other character choices.

What's going on

The two rules are at least eight characters and at least one digit. len checks the first rule; any scans character tests until a digit is found or the response ends.

Both booleans must be true for the valid message. If validation fails, two separate if statements explain whichever rules failed. This is a classroom validator, not password storage or a complete security policy.

Can both error messages appear? Yes. A short password without a digit fails both independent checks.

Does eight spaces and a digit pass? Yes. The code has no restriction on spaces or other character choices.

Quiz

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

Question 1

Why use any for digits?

Pick an answer to check it.

Question 2

Can both error messages appear?

Pick an answer to check it.

Questions people ask

Can both error messages appear?

Yes. A short password without a digit fails both independent checks.

Does eight spaces and a digit pass?

Yes. The code has no restriction on spaces or other character choices.

Why use any for digits?

One matching character is enough.