Valid Foundations-of-Programming-Python Dumps shared by EduDump.com for Helping Passing Foundations-of-Programming-Python Exam! EduDump.com now offer the newest Foundations-of-Programming-Python exam dumps, the EduDump.com Foundations-of-Programming-Python exam questions have been updated and answers have been corrected get the newest EduDump.com Foundations-of-Programming-Python dumps with Test Engine here:
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
Correct Answer:
See the Step by Step Solution below in Explanation. Explanation: 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