# Learn String Manipulation by Building a Cipher - Step 57

**URL:** <https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554>\
**Category:** General\
**Tags:** português\
**Created:** [January 23, 2024, 4:42pm UTC](https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554 "2024-01-23T16:42:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramonsilva](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@ramonsilva](https://forum.freecodecamp.org/u/ramonsilva)\
**Post date:** [January 23, 2024, 4:42pm UTC](https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554/1 "2024-01-23T16:42:28Z")

</div>

### Conte-nos o que está acontecendo:

Descreva detalhadamente o problema aqui.

### Seu código até o momento

```py

text = 'Hello Zaira'
custom_key = 'python'

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

    for char in message.lower():

# User Editable Region

        # Append space to the message
        if char == ' ':
            encrypted_text += char
        else: key_char = key

# User Editable Region

            index = alphabet.find(char)
            new_index = (index + offset) % len(alphabet)
            encrypted_text += alphabet[new_index]
    print('plain text:', message)
    print('encrypted text:', encrypted_text)

```

### Informações do seu navegador:

Agente de usuário: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36`

### Informações do desafio:

Learn String Manipulation by Building a Cipher - Step 57

> **[freeCodeCamp.org](https://www.freecodecamp.org/portuguese/learn/scientific-computing-with-python/learn-string-manipulation-by-building-a-cipher/step-57)**
>
> Aprenda a programar — de graça

Next, inside the `else` block, declare a variable called `key_char` and assign it the value of `key` at the index `key_index` mod(`%` ) the length of `key` . what is to do???

---

<div class="post-metadata">

**Author:** ![DobarBREND](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dobarbrend/32/297954_2.png) [@DobarBREND](https://forum.freecodecamp.org/u/DobarBREND)\
**Post date:** [January 23, 2024, 5:24pm UTC](https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554/2 "2024-01-23T17:24:52Z")

</div>

> [@ramonsilva](#):
>
> ```auto
> else: key_char = key
> 
> ```

You need to add the index to the ‘key’. Use the bracket notation to do that. Add the `[]` brackets right after the word ‘key’. Inside these brackets put the formula for searching the required index according to the instructions:

```auto
your_new_variable = second_parameter_of_vigenere[your_new_variable % length_of_the(second_parameter_of_vigenere)]

```

This is guidance.

P.S.  
The ‘modulo’ explained:

> **[What is Modulo?](https://www.computerhope.com/jargon/m/modulo.htm)**
>
> Computer dictionary definition of what modulo means, including related links, information, and terms.

---

<div class="post-metadata">

**Author:** ![ramonsilva](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@ramonsilva](https://forum.freecodecamp.org/u/ramonsilva)\
**Post date:** [January 23, 2024, 10:32pm UTC](https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554/3 "2024-01-23T22:32:46Z")

</div>

> [@DobarBREND](#):
>
> You need to add the index to the ‘key’. Use the bracket notation to do that. Add the `[]` brackets right after the word ‘key’. Inside these brackets put the formula for searching the required index according to the instructions

# Append space to the message

```
    if char == ' ':
        encrypted_text += char
    else: key_char = key[key_char % 26(key)]
      what's wrong in the code?

```

---

<div class="post-metadata">

**Author:** ![DobarBREND](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dobarbrend/32/297954_2.png) [@DobarBREND](https://forum.freecodecamp.org/u/DobarBREND)\
**Post date:** [January 24, 2024, 3:52pm UTC](https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554/4 "2024-01-24T15:52:30Z")

</div>

> [@ramonsilva](#):
>
> `else: key_char = key[key_char % 26(key)]`

After ` else:` move the code one row below and take care of indentation (must be the same as `encrypted_text += char`  
Instead of the number 26, put the name of the length function `len`.

---

<div class="post-metadata">

**Author:** ![Dave12](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@Dave12](https://forum.freecodecamp.org/u/Dave12)\
**Post date:** [January 27, 2024, 2:42pm UTC](https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554/5 "2024-01-27T14:42:54Z")

</div>

```auto
        if char == ' ':
            encrypted_text += char
        else:
           key_char = key[key_index % len(key)]

```

I have done everything but still can’t get the result

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 27, 2024, 4:40pm UTC](https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554/6 "2024-01-27T16:40:58Z")

</div>

your code passes for me, please show all the code you have in the editor

I’ve edited your code for readability. 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.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (').

---

<div class="post-metadata">

**Author:** ![DobarBREND](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dobarbrend/32/297954_2.png) [@DobarBREND](https://forum.freecodecamp.org/u/DobarBREND)\
**Post date:** [January 27, 2024, 4:50pm UTC](https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554/7 "2024-01-27T16:50:33Z")

</div>

> [@Dave12](#):
>
> ```auto
> if char == ' ':
> encrypted_text += char
> else:
> key_char = key[key_index % len(key)]
> 
> ```

Pay attention to indentation. The `key_char = key[key_index % len(key)]` is not aligned with the `encrypted_text += char`.

---

<div class="post-metadata">

**Author:** ![Dave12](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@Dave12](https://forum.freecodecamp.org/u/Dave12)\
**Post date:** [January 27, 2024, 5:27pm UTC](https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554/8 "2024-01-27T17:27:45Z")

</div>

Thank you I have already done that!

---

<div class="post-metadata">

**Author:** ![Dave12](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@Dave12](https://forum.freecodecamp.org/u/Dave12)\
**Post date:** [January 27, 2024, 5:28pm UTC](https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554/9 "2024-01-27T17:28:15Z")

</div>

The code works perfectly fine!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 7, 2025, 3:37pm UTC](https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-57/666554/10 "2025-01-07T15:37:17Z")

</div>

Closed on January 7, 2025, 3:37pm UTC


