Skip to content

Commit

Permalink
Merge pull request #46 from mrsandy1965/patch-1
Browse files Browse the repository at this point in the history
Update solution.py
  • Loading branch information
Kushal997-das authored Oct 22, 2024
2 parents 069011b + ce7c311 commit 2c0412a
Showing 1 changed file with 37 additions and 49 deletions.
86 changes: 37 additions & 49 deletions Beginner Level 📁/Password Strength Checker/solution.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,42 @@
password = input("Enter a password: ")
def check_password_strength():
try:
password = input("Enter a password: ")

# Check the length of the password
length = len(password)
# Check if the password is empty
if not password:
raise ValueError("Password cannot be empty.")

# Initialize
has_upper = False
has_lower = False
has_digit = False
has_symbol = False
# Check the length of the password
length = len(password)

# Loops each character in password to check
for char in password:
if 'a' <= char <= 'z':
# Checks for lowercase
has_lower = True
elif 'A' <= char <= 'Z':
# Checks for uppercase
has_upper = True
elif "0" <= char <= '9':
# Checks for digit
has_digit = True
else:
# Checks for symbol
has_symbol = True
# Initialize character type flags
has_upper = any(char.isupper() for char in password)
has_lower = any(char.islower() for char in password)
has_digit = any(char.isdigit() for char in password)
has_symbol = any(not char.isalnum() for char in password)

# Logic conditons
if length < 6:
# Less than 6 characters
ans = "weak"
elif length >= 6 and length <= 10:
# between 6 and 10 characters
if (has_lower and has_upper) or (has_digit and (has_lower or has_upper)):
# Contains lowercase and uppercase letters or both letters and digits
ans = "moderate"
elif has_lower and not has_upper and not has_digit and not has_symbol:
# Contains only lowercase letters
ans = "weak"
elif has_digit and not has_lower and not has_upper and not has_symbol:
# Contains only digits
ans = "weak"
else:
ans = "weak"
elif length > 10:
# Longer than 10 characters
if (has_lower and has_upper) or (has_digit and (has_lower or has_upper)):
ans = "moderate"
elif has_lower and has_upper and has_digit and has_symbol:
# Contains lowercase, uppercase, digits, and special characters
ans = "strong"
else:
ans = "weak"
# Determine password strength
if length < 6:
ans = "weak"
elif 6 <= length <= 10:
if (has_lower and has_upper) or (has_digit and (has_lower or has_upper)):
ans = "moderate"
else:
ans = "weak"
else: # length > 10
if has_lower and has_upper and has_digit and has_symbol:
ans = "strong"
elif (has_lower and has_upper) or (has_digit and (has_lower or has_upper)):
ans = "moderate"
else:
ans = "weak"

print(f'Your password is {ans}.')
print(f'Your password is {ans}.')

except ValueError as ve:
print(f"Error: {ve}")
except Exception as e:
print(f"An unexpected error occurred: {e}")

# Call the function to execute the password strength check
check_password_strength()

0 comments on commit 2c0412a

Please sign in to comment.