Stuck on step 17: Move body of function into for loop

Tell us what’s happening:

Your code so far

def pin_extractor(poems):
    secret_code = "" 
    for poem in poems: 
        lines = poem.split('\n')
        for line_index, line in enumerate(lines):
            words = line.split() 
            if len(words) > line_index: 
                secret_code += str(len(words[line_index])) 
            else: 
                secret_code += '0'
     return secret_code
poem = """Stars and the moon shine in the sky white and bright until the end of the night"""
poem2 = 'The grass is green\nhere and there\nhoping for rain\nbefore it turns yellow' poem3 = 'There\nonce\nwas\na\ndragon'
print(pin_extractor(poem))

I am stuck on Step 17. I keep getting the error “You should move the existing body of the function into the new for loop”. Can someone please check my code?

Lesson URL (copy - paste from your browser’s address bar)

Welcome to the forum @marwakabara6,

Is this line inside the loop?


In the future, if you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

@dhess have updated the code and put all the lines inside the for loop, but I am still getting the same error: “You should move the existing body of the function into the new for loop”. Can someone please tell me exactly what is wrong with my indentation?

You may want to reset this step and try again.

Otherwise, please post your updated code formatted as follows:

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

`type or paste code here`

def pin_extractor(poems):

for poem in poems:

    secret_code = ""

    lines = poem.split('\\n')

    for line_index, line in enumerate(lines):

        words = line.split()

        if len(words) > line_index:

            secret_code += str(len(words\[line_index\]))

        else:

            secret_code += '0'

return secret_code

poem = “”"Stars and the moon

shine in the sky

white and

until the end of the night"“”

poem2 = ‘The grass is green\nhere and there\nhoping for rain\nbefore it turns yellow’

poem3 = ‘There\nonce\nwas\na\ndragon’

# print(pin_extractor(poem))Preformatted textPreformatted text

@dhess Actually, it is still not working in my editor. The error persists. Can you check my code again?

Could’ve sworn you marked that post as a solution. :person_shrugging:

I can’t test your code because it wasn’t formatted correctly. Can you try posting it again?

The easiest way I’ve found to do that is to enter 3 backticks (```) on a new line, then copy the code and paste it beneath that line…then you can close off the code block with another 3 backticks.

@dhess I have attached screenshots of my current code and the error message. Could you please take a look and tell me exactly what is wrong? I am still stuck on this step and cannot pass it. Thank you for your help!:slightly_smiling_face:

I also can’t test from a screenshot. Your code looks okay to me. That’s why I need to test it in the editor. If it works for me, but isn’t passing for you, you may need to take some extra steps.

,,,
def pin_extractor(poems):
    for poem in poems:
        secret_code = ""
        lines = poem.split('\n')
        for line_index, line in enumerate(lines):
            words = line.split()
            if len(words) > line_index:
                secret_code += str(len(words[line_index]))
            else:
                secret_code += '0'
    return secret_code
poem = """Stars and the moon
shine in the sky
white and
until the end of the night"""

poem2 = 'The grass is green\nhere and there\nhoping for rain\nbefore it turns yellow'
poem3 = 'There\nonce\nwas\na\ndragon'

# print(pin_extractor(poem))

@dhess I have formatted the code correctly this time. Can you please test it now and tell me if there is any error? I am still stuck and I really appreciate your help.

Did you ever reset this step? The last line of code in the function is not aligned correctly.

@dhess Thank you so much, dhess! You were absolutely right. The return statement was not aligned correctly. I fixed the indentation and the code finally passed!

I really appreciate your patience and help with this. I couldn’t have solved it without you. Have a fantastic day!:clap: