Stuck on Step 49 – Scientific Computing with Python (function definition)

Hi everyone,

I’ve been stuck on Step 49 of the Scientific Computing with Python course for almost two weeks. The step asks me to define a function called caesar() after declaring a shift variable, and indent the following lines to give the function a body.

Here’s my current code:

python

CopyEdit

text = 'Hello Zaira'
shift = 3
    def caesar():
        alphabet = 'abcdefghijklmnopqrstuvwyz'
        encrypted_text = ''

        for char in text.lower():
            if char == ' ':
                encrypted_text += char
            eles:
                index = alphabet.find(char)
                new_index = (index + shift) % len(alphabet)
                encryted_text += alphabet[new_index]

        print('encrypted text:', encrypted_text)
        print('plain text:', text)

    caesar()

I’ve tried everything I can think of, but it’s still not working—can someone point out what I’m missing or doing wrong?

Thanks in advance!

Why the indentation here? That’s a problem for you.

but then its gives me this error You should indent all the lines after

shift = 3

so that they become your new function body.

Please post all of your updated code. Also, please post a link to this step.

Please stop posting messages written by AI

i wrote that code though and you told me to send the link ?
so thats what i did

You sent a message written by AI. Please do not do that.

Please post your code and a link to the Step without posting a message written by AI.

Learn String Manipulation by Building a Cipher: Step 49 | freeCodeCamp.org

  text = 'Hello Zaira'
shift = 3

def caesar():
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    encrypted_text = ''

    for char in text.lower():
        if char == ' ':
            encrypted_text += char
        else:
            index = alphabet.find(char)
            new_index = (index + shift) % len(alphabet)
            encrypted_text += alphabet[new_index]

    print('encrypted text:', encrypted_text)
    print('plain text:', text)

I’ve edited your post to improve the readability of the code. 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 (').

text = 'Hello Zaira'
shift = 3

def caesar():
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    encrypted_text = ''

    for char in text.lower():
        if char == ' ':
            encrypted_text += char
        else:
            index = alphabet.find(char)
            new_index = (index + shift) % len(alphabet)
            encrypted_text += alphabet[new_index]

    print('encrypted text:', encrypted_text)
    print('plain text:', text)

I’ve edited your post to improve the readability of the code. 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 (').

It looks like you changed the code in the body of your function. You must make no changes to the code. Only indent the code that was already there.

If you are using AI to do parts of this for you, I would recommend you stop. It’s pretty common for AI to make little mistakes like that.

even when I do it on my own it still doesn’t go through.

You need to reset the code and only add the def caesar(): part and indentation. Make no other changes.

Alright, thank you I’ll give it a try.

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