Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

WGU Foundations of Programming Python Exam - Topic 3 Question 11 Discussion

SIMULATIONWrite 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 criteriaif len(password) < 8:return "Weak"has_letter = Falsehas_number = Falsefor char in password:if char.isalpha():has_letter = Trueelif char.isdigit():has_number = True# TODO: Add your return logic here based on has_letter and has_numberpass
A) See the Step by Step Solution below in Explanation

WGU Foundations of Programming Python Exam - Topic 3 Question 11 Discussion

Actual exam question for WGU's Foundations of Programming Python exam
Question #: 11
Topic #: 3
[All Foundations of Programming Python Questions]

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

Show Suggested Answer Hide Answer
Suggested Answer: A

==========

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


Contribute your Thoughts:

0/2000 characters
Arlene
1 day ago
Wait, only letters and numbers? That seems too simple!
upvoted 0 times
...
Cortney
6 days ago
This is a good start, but I feel like it needs more checks.
upvoted 0 times
...
Wilda
11 days ago
I thought it should be at least 10 characters for strong?
upvoted 0 times
...
Izetta
17 days ago
Definitely agree, but what about special characters?
upvoted 0 times
...
Stevie
22 days ago
Looks like a solid way to check password strength!
upvoted 0 times
...
Billye
27 days ago
I think I need to double-check how to use the `isalpha()` and `isdigit()` methods correctly in the loop.
upvoted 0 times
...
Terry
1 month ago
I feel a bit uncertain about how to handle the return logic. Do we need to check both conditions before returning?
upvoted 0 times
...
Ellsworth
1 month ago
This seems similar to that exercise where we validated usernames. I think we just need to add a return statement at the end.
upvoted 0 times
...
Gretchen
1 month ago
I remember we practiced checking for letters and numbers in strings, but I'm not sure how to combine those checks.
upvoted 0 times
...

Save Cancel