#Python

8 messages · Page 1 of 1 (latest)

undone pelican
#

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()

#

dunno if it works, I'm just copying what my ex sent

#

define the has_special_char() function that takes a password as input
create a string special_chars containing all the special characters we want to check for
loop through each character in the password
If any character is found in our special_chars string, we return True immediately
If we finish the loop without finding any special characters, we return False

This code will tell you whether each test password contains special characters or not. For example:

"Hello123" → No special characters
"Pass^word" → Contains special characters (^)
"H3ll0!" → Contains special characters (!)

tall vault
#

i'm assuming the second part of your message is your solution

#
def has_special_char(password):
    special_char = "!*^%^%"
    # loops through each character in the string
    # logic: it checks each character in the password string,
    # if there are no special characters, the loop finishes and returns false
    for char in password:
        # checks if the character we are looping over is in special_char
        if char in special_char:
            return True
    return False

# There is no "function" keyword in python, use "def"
function has_special_char(password)
    # Naming inconsistency (1) special_chars
    set special_chars = "!*^%^%"
    # No need for "each"
    for each character in password
        # Naming inconsistency (2) special_char
        if character is in special_char then 
                # Boolean expressions in python uses True/False instead
                return true
    return false 
#

i wrote some comments explaining the solution and wrote comments on which parts you could change

#

also idk if you were stuck on just this part of the problem or if you were asking about the project and driver method too