Back to Freecodecamp

Step 39

curriculum/challenges/english/blocks/learn-string-manipulation-by-building-a-cipher/65525e359ca28d938baa82c5.md

latest1.2 KB
Original Source

--description--

You can obtain the same effect of a = a + b by using the addition assignment operator:

py
a += b

The addition assignment operator enables you to add a value to a variable and then assign the result to that variable.

Use the += operator to add a value and assign it at the same time to encrypted_text.

--hints--

You should use the addition assignment operator to add alphabet[new_index] to the current value of encrypted_text inside your loop body.

js
({ test: () => {
    const commentless_code = __helpers.python.removeComments(code);
    const {block_body} = __helpers.python.getBlock(commentless_code, /for\s+char\s+in\s+text\.lower\s*\(\s*\)\s*/);
    assert(block_body.match(/encrypted_text\s*\+=\s*alphabet\s*\[\s*new_index\s*\]/));
  }
})

--seed--

--seed-contents--

py
text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted_text = ''
--fcc-editable-region--
for char in text.lower():
    index = alphabet.find(char)
    new_index = index + shift
    encrypted_text = encrypted_text + alphabet[new_index]
    print('char:', char, 'encrypted text:', encrypted_text)
--fcc-editable-region--