Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Password Checker
#9
Since people are submitting solutions now, I thought I might demonstrate a different approach. This is a pass/fail implementation with feedback. Get comfortable with Python exceptions. They are very useful.
def set_password(password):
    if len(password) < 7:
        raise ValueError("Password must be at least 7 characters")

    if not any(map(str.islower, password)):
        raise ValueError("Password must contain a lowercase letter")

    if not any(map(str.isupper, password)):
        raise ValueError("Password must contain an uppercase letter")

    if not any(map(str.isdigit, password)):
        raise ValueError("Password must contain a digit")

    if not (set(password) & set('!@#$%^&*()')):
        raise ValueError("Password must contain a special character")

    # Code that sets the password goes here

while password := input('Enter New Password: '):
    try:
        set_password(password)
        break
    except ValueError as msg:
        print(msg)
This computes a strength score.
def password_strength(password):
    strength = 0 if len(password) < 7 else 1

    if any(map(str.islower, password)):
        strength += 1

    if any(map(str.isupper, password)):
        strength += 1

    if any(map(str.isdigit, password)):
        strength += 1

    if set(password) & set('!@#$%^&*()'):
        strength += 1

    return strength

while password := input('Enter New Password: '):
    print(password_strength(password))
And if you love unreadable one-liners:
def valid_password(p):
    return all([len(p) > 0, any(map(str.islower, p)), any(map(str.isupper, p)), any(map(str.isdigit, p)), set(p) & set('!@#$%^&*()')])

while password := input('Enter New Password: '):
    print(valid_password(password))
It is good to know about any() and all(). To use any() or all() affectively it is also good to know about map() and list comprehensions.
Dexty likes this post
Reply


Messages In This Thread
Simple Password Checker - by Dexty - Sep-21-2021, 09:29 PM
RE: Simple Password Checker - by deanhystad - Sep-21-2021, 09:35 PM
RE: Simple Password Checker - by BashBedlam - Sep-21-2021, 10:50 PM
RE: Simple Password Checker - by Dexty - Sep-23-2021, 09:54 PM
RE: Simple Password Checker - by bowlofred - Sep-22-2021, 12:18 AM
RE: Simple Password Checker - by jamesaarr - Sep-23-2021, 10:57 AM
RE: Simple Password Checker - by SamHobbs - Sep-23-2021, 11:11 PM
RE: Simple Password Checker - by Dexty - Sep-24-2021, 06:17 AM
RE: Simple Password Checker - by deanhystad - Sep-24-2021, 06:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable not defined in password checker DAS 4 5,883 Aug-27-2017, 08:40 PM
Last Post: ichabod801
  Password checker sammy2938 2 11,968 Jul-19-2017, 08:04 PM
Last Post: nilamo
  Email Checker with IMAPlib ronaldrios 1 4,682 Jun-23-2017, 04:03 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020