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

Tell us what’s happening:

I have given the input the code is not getting executed can someone please help me in this

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.lower() for char in pascal_or_camel_cased_string if char.isupper() else char in pascal_or_camel_string]

# 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

Challenge Information:

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

you have added a new list comprehension, instead reset the step, move the if part, and add the else part following the syntax in the exampe, you do not need to add anything else

so once i reset i have to add if and else part at the end of the code right?

Once you have reset the code you need to tweak it to add an else statement. Note you have to move the if statement as well to before the for keyword if you are also adding an else statement.

Look at the example code and apply the syntax for an if else statement to your code.

spam = [i * 2 if i > 0 else -1 for i in iterable]

You will need to add an expression to go in the else statement as set out in the instructions. You will need to work out how to achieve this in your code.

Modify your list comprehension so that when a character is not uppercase it remains unchanged.

can you please elabaote on the step as i am not sure when to put the else statement in the code

Looking at the example for an if else statement in step 21:

spam = [i * 2 if i > 0 else -1 for i in iterable]

In step 20 the example for an if statement on its own:

spam = [i * 2 for i in iterable if i > 0]

Can you see how the if statement has moved in the example code in step 21? if i > 0 is moved to after the initial expression.

You need to turn your existing line of code at step 21 into an if else statement. This involves 2 things:

  1. Moving your if statement to the new position. Don’t change it otherwise.
  2. Adding an else statement after the if statement once you have moved it. This will take a bit more thinking.

Take each step one at a time - do the step 1 first and post your code back here. Put backticks in the forum before and after your code (```).

ok but what should i write in else statement?

the instructions are telling that to you

Modify your list comprehension so that when a character is not uppercase it remains unchanged.

got it thanks for the help