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.
1# Take password2password = input("Enter password: ")34# Check rules5has_length = len(password) >= 86has_digit = any(char.isdigit() for char in password)78if 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
Read the password and calculate both booleans.
- 2
Accept only if length and digit checks pass.
- 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.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum