Learn Python List Comprehension by Building a Case Converter Program - Step 19

Tell us what’s happening:

stuck here for an hour, tried googled and to no avail still can’t get it right. I don’t know where I’m wrong, help.

Your code so far

def convert_to_snake_case(pascal_or_camel_cased_string):
    snake_cased_char_list = []
    for char in pascal_or_camel_cased_string:
        if char.isupper():
          converted_character = '_' + char.lower()
          snake_cased_char_list.append(converted_character)
        else:
            snake_cased_char_list.append(char)
    snake_cased_string = ''.join(snake_cased_char_list)
    clean_snake_cased_string = snake_cased_string.strip('_')

    return clean_snake_cased_string


# User Editable Region

    snake_cased_char_list = [char for char in pascal_or_camel_cased_string]
    pascal_or_camel_cased_string = '_' + char.lower()


# User Editable Region

    return ''.join(snake_cased_char_list).strip('_')

def main():
    print(convert_to_snake_case('aLongAndComplexString'))

main()

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0

Challenge Information:

Learn Python List Comprehension by Building a Case Converter Program - Step 19

Hey, you are creating a list with the characters from pascal_or_camel_cased_string, but you are not modifying them.

Instead of char (the first one I mean) you should concatenate _ and char.lower().
Of course, you need to remove this

1 Like

Hello! thank you for the reply. I made some changes but I’m still stuck. I have a strong feeling the fault is at _ and char.lower(). But I can’t figure out what gone wrong?
my changes is
““snake_cased_char_list = [char.lower + ‘_’ for char in pascal_or_camel_cased_string]””

you are not calling the lower() function

1 Like

Hello! Thank you for pointing that very simple mistake, I fixed it but I still can’t get it right
new changes is
““snake_cased_char_list = [char.lower() + ‘_’ for char in pascal_or_camel_cased_string]””

Try to swap the order.

Changed but still can’t get it right? Should I put them in a () ?
changes
““snake_cased_char_list = [‘_’ + char.lower() for char in pascal_or_camel_cased_string]””

Please share the entire updated code.

1 Like

I don’t know how to do that. Should I copy & paste the entire code?

yes, exactly that

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

def convert_to_snake_case(pascal_or_camel_cased_string):
    snake_cased_char_list = []
    for char in pascal_or_camel_cased_string:
        if char.isupper():
          converted_character = '_' + char.lower()
          snake_cased_char_list.append(converted_character)
        else:
            snake_cased_char_list.append(char)
    snake_cased_string = ''.join(snake_cased_char_list)
    clean_snake_cased_string = snake_cased_string.strip('_')

    return clean_snake_cased_string
    snake_cased_char_list = ['_' + char.lower() for char in pascal_or_camel_cased_string]
    return ''.join(snake_cased_char_list).strip('_')

def main():
    print(convert_to_snake_case('aLongAndComplexString'))

main()

you started the step with a lot of lines commented out, they still need to be commented out

1 Like

Finally got it right! Thank you!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.