SIMULATION
Write a complete function password_strength(password) that returns "Strong" if the password is at least 8 characters long and contains both letters and numbers, "Weak" otherwise.
For example, password_strength("abc123def") should return "Strong".
def password_strength(password):
# TODO: Return "Strong" or "Weak" based on password criteria
if len(password) < 8:
return "Weak"
has_letter = False
has_number = False
for char in password:
if char.isalpha():
has_letter = True
elif char.isdigit():
has_number = True
# TODO: Add your return logic here based on has_letter and has_number
pass
==========
Step 1: First, check the password length using len(password).
Step 2: If the password has fewer than 8 characters, return 'Weak' immediately.
Step 3: Create two Boolean variables: has_letter and has_number.
Step 4: Loop through each character in the password.
Step 5: Use .isalpha() to check for letters and .isdigit() to check for numbers.
Step 6: If the password contains both at least one letter and at least one number, return 'Strong'.
Step 7: Otherwise, return 'Weak'.
Correct code:
def password_strength(password):
if len(password) < 8:
return 'Weak'
has_letter = False
has_number = False
for char in password:
if char.isalpha():
has_letter = True
elif char.isdigit():
has_number = True
if has_letter and has_number:
return 'Strong'
else:
return 'Weak'
Example:
print(password_strength('abc123def'))
print(password_strength('abcdefgh'))
print(password_strength('12345678'))
Output:
Strong
Weak
Weak
Arlene
1 day agoCortney
6 days agoWilda
11 days agoIzetta
17 days agoStevie
22 days agoBillye
27 days agoTerry
1 month agoEllsworth
1 month agoGretchen
1 month ago