-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from mrsandy1965/patch-1
Update solution.py
- Loading branch information
Showing
1 changed file
with
37 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |