Learn String Manipulation by Building a Cipher - Step 59

Tell us what’s happening:

Describe your issue in detail here.
I don’t know where to put this code “def key_index(0):”
It says " You should declare a variable called key_index at the beginning of your function body."

Your code so far


# User Editable Region

text = 'Hello Zaira'
custom_key = 'python'

def vigenere(message, key):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    encrypted_text = ''
    
def key_index(0):
    for char in message.lower():
        if char == ' ':
            encrypted_text += char
        else:
            index = alphabet.find(char)
            new_index = (index + offset) % len(alphabet)
            encrypted_text += alphabet[new_index]
    print('plain text:', message)
    print('encrypted text:', encrypted_text)

# 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/123.0.0.0 Safari/537.36

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 59

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.
Learning to describe problems is hard, but it is an important part of learning how to code.
Also, the more you say, the more we can help!

That’s the beginning of a function definition, if we ignore the 0. What it is asking is for a variable name. You have been working all along with a few variable names. Here’s an example.

text = 'Hello Zaira'

It is asking to create a variable name key_index and to assign the value of zero. It also wants you to do it in the beginning of a function (you only have one at this point: vigenere)
Mind your indentation when inside a function body.

text = 'Hello Zaira'
custom_key = 'python'
***key_index = 0***

def vigenere(message, key):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    encrypted_text = ''
    
    for char in message.lower():
        if char == ' ':
            encrypted_text += char
        else:
            index = alphabet.find(char)
            new_index = (index + offset) % len(alphabet)
            encrypted_text += alphabet[new_index]
    print('plain text:', message)
    print('encrypted text:', encrypted_text)

Is this how it should be, I am confused.

key_index = 0 has to go to the very beggining of the function declaration. So:

– removed –

In my case, the space below encrypted_text brought me a lot of confusion. I think there is an error in the edition of the exercise.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

This means indented after the define function line def vigenere(message, key): where you define other variables alphabet and encrypted_text