Learn String Manipulation by Building a Cipher - Step 49

Tell us what’s happening:

i can not under stand whats wrong with my code why give me this massage(Your code raised an error before any tests could run. Please fix it and try again.)

Your code so far


# User Editable Region

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('plain text:', text)
print('encrypted text:', encrypted_text)

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; rv:107.0) Gecko/20100101 Firefox/107.0

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 49

In the terminal you can see what’s going on:

Traceback (most recent call last):
  File "main.py", line 16, in <module>
NameError: name 'encrypted_text' is not defined

This line

is outside the function body, where encrypted_text is defined.
Variables defined inside a function are not available from the outside.
You need to indent those last two lines, too.

Also, pay attention to the amount of indentation you use:
image
Those vertical lines represent the correct indentation (4 spaces by convention).
For some reason you are using 5 spaces. Make sure to have something like this instead:
image

i do this and still give me this massage( plain text: Hello Zaira
Traceback (most recent call last):
File “main.py”, line 14, in
NameError: name ‘encrypted_text’ is not defined

)

Please, share your updated code.

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('plain text:', text)
print('encrypted text:', encrypted_text)

Enclose your code between triple backticks. Otherwise I cannot see the indentation.

your print is still outside the function

i can not understand you

How can i do that i am sorry??

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('plain text:', text)
print('encrypted text:', encrypted_text)

[/quote]

you indented many lines so that they are inside the function, you need to also indent the last two lines tand put them inside the function

OK I do that but still says ( You should indent all the lines after shift = 3 so that they become your new function body.)

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('plain text:', text)
        print('encrypted text:', encrypted_text)

[/quote]

Reset the step and look at the original indentation. You can’t change the relative indentation of the lines, you have to maintain that structure, otherwise your print statements become part of the else: block when they were not in there before.

its work ,thanks a lot :heart: :heart: :heart: :heart: :heart:

1 Like