def has_special_char(password):
# Define the special characters we want to check for
special_chars = "!*^%#@$&"
# Loop through each character in the password
for char in password:
# Check if the current character is in our special_chars string
if char in special_chars:
return True
# If we've gone through all characters and found no special ones
return False
Example usage:
def test_password():
# Test cases
passwords = ["Hello123", "Pass^word", "NoSpecial", "H3ll0!"]
for pwd in passwords:
if has_special_char(pwd):
print(f"'{pwd}' contains special characters")
else:
print(f"'{pwd}' does not contain special characters")
Run test cases
test_password()