Back to Freecodecamp

Step 46

curriculum/challenges/english/blocks/learn-string-manipulation-by-building-a-cipher/655619327c7b364166f8dd6f.md

latest1.1 KB
Original Source

--description--

If you wish to incorporate additional characters into the alphabet string, such as digits or special characters, you'll find it's necessary to manually modify the right operand of the modulo operation.

Replace 26 with len(alphabet) to avoid this issue.

--hints--

You should modify the new_index value replacing 26 with len(alphabet).

js
({ test: () => {
  const commentless_code = __helpers.python.removeComments(code);
  const {block_body} = __helpers.python.getBlock(commentless_code, /else/);
  assert(block_body.match(/new_index\s*=\s*\(\s*index\s*\+\s*shift\s*\)\s*%\s*len\s*\(\s*alphabet\s*\)/));
} })

--seed--

--seed-contents--

py
--fcc-editable-region--
text = 'Hello Zaira'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted_text = ''

for char in text.lower():
    if char == ' ':
        encrypted_text += char
    else:
        index = alphabet.find(char)
        new_index = (index + shift) % 26
        encrypted_text += alphabet[new_index]
    print('char:', char, 'encrypted text:', encrypted_text)
--fcc-editable-region--