Learn String Manipulation by Building a Cipher - Step 70

Tell us what’s happening:

Hi everyone,
I’m stuck on Step 70 of the Vigenère Cipher project. I’ve added the third parameter direction to the function and commented out the last two lines exactly as instructed, but it still says “Your code does not pass.”

I’ve tried resetting and rewriting carefully, but can’t figure out what’s wrong.
Any guidance would be really appreciated!

Thank you in advance :folded_hands:

Your code so far


# User Editable Region

text = 'Hello Zaira'
custom_key = 'python'

def vigenere(message, key, direction):
    key_index = 0
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    encrypted_text = ''

    for char in message.lower():
        # Append space to the message
        if char == ' ':
            encrypted_text += char
        else:
            # Find the right key character to encode
            key_char = key[key_index % len(key)]
            key_index += 1

            # Define the offset and the encrypted letter
            offset = alphabet.index(key_char)
            index = alphabet.find(char)
            new_index = (index + offset) % len(alphabet)
            encrypted_text += alphabet[new_index]
    
    return encrypted_text
    

#encryption = vigenere(text, custom_key, 'encode')
#print('encrypted text:', encryption)


# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 70

the last two lines have to be commented out without changing them

Hi, I’m stuck on Step 70 of the Vigenère Cipher project.

I have followed all the instructions carefully:

  • I added the third parameter direction to the function definition.
  • I did not change the last two lines — I only added # in front of them to comment them out exactly as they were.

But I’m still getting this error:

Sorry, your code does not pass. You’re getting there.
You should turn the last two lines in your code into comments. Put a # at the beginning of each line.

I’ve even tried resetting the code and carefully retyping it. Still the same error.

Can someone please help me figure out what might be going wrong?

Thanks in advance!

Welcome to the forum @ishratnawaz

So the forum can assist, please post your latest code.
Make sure you only comment out the code, do not modify it in any way.

Happy codnig

text = 'Hello Zaira'
custom_key = 'python'

def vigenere(message, key, direction):
    key_index = 0
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    encrypted_text = ''

    for char in message.lower():
        # Append space to the message
        if char == ' ':
            encrypted_text += char
        else:
            # Find the right key character to encode
            key_char = key[key_index % len(key)]
            key_index += 1

            # Define the offset and the encrypted letter
            offset = alphabet.index(key_char)
            index = alphabet.find(char)

            if direction == 'encode':
                new_index = (index + offset) % len(alphabet)
            else:
                new_index = (index - offset) % len(alphabet)

            encrypted_text += alphabet[new_index]

    return encrypted_text

# encryption = vigenere(text, custom_key, 'encode')
# print('encrypted text:', encryption)

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 (').

They are changed, you may want to reset the step

Hi everyone, I just wanted to follow up — I’ve tried to format everything as instructed using triple backticks.

I’m stuck at Step 70 of the Caesar Cipher → Vigenère project. I’ve added the third parameter (direction) and commented out the last two lines as requested, but it still says my code is incorrect.

I’ve pasted my current version of the code above.

Would appreciate any help or insight to figure out what I’m missing! :folded_hands:

text = 'Hello Zaira'
custom_key = 'python'

def vigenere(message, key, direction):
    key_index = 0
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    encrypted_text = ''

    for char in message.lower():
        # Append space to the message
        if char == ' ':
            encrypted_text += char
        else:
            # Find the right key character to encode
            key_char = key[key_index % len(key)]
            key_index += 1

            # Define the offset and the encrypted letter
            offset = alphabet.index(key_char)
            index = alphabet.find(char)

            if direction == 'encode':
                new_index = (index + offset) % len(alphabet)
            else:
                new_index = (index - offset) % len(alphabet)

            encrypted_text += alphabet[new_index]

    return encrypted_text

# encryption = vigenere(text, custom_key, 'encode')
# print('encrypted text:', encryption)

these are still changed, you should reset the step