Back to Freecodecamp

Step 43

curriculum/challenges/english/blocks/learn-string-manipulation-by-building-a-cipher/6553995f412dd8122ed38e4a.md

latest1.8 KB
Original Source

--description--

A conditional statement can also have an else clause. This clause can be added to the end of an if statement to execute alternative code if the condition of the if statement is false:

py
if x != 0:
    print(x)
else:
    print('x = 0')

As you can see in your output, when the loop iterations reach the space, a space is added to the encrypted string. Then the code outside the if block executes and a c is added to the encrypted string.

To fix it, add an else clause after encrypted_text += char and indent all the subsequent lines of code except the print() call.

--hints--

You should create an else clause. Remember to include the final colon.

js
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(/else\s*:/));

You should indent the lines of code after your else clause except the print() call.

js
const commentless_code = __helpers.python.removeComments(code);
const {block_body} = __helpers.python.getBlock(commentless_code, /else/);
assert(block_body.match(/index\s*=\s*alphabet\.find\s*\(\s*char\s*\)\s*new_index\s*=\s*index\s*\+\s*shift\s*encrypted_text\s*\+=\s*alphabet\s*\[\s*new_index\s*\]\s*$/));

Your code contains invalid syntax and/or invalid indentation.

js
({test: () => assert(true) })

--seed--

--seed-contents--

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

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