Back to Freecodecamp

Step 80

curriculum/challenges/english/blocks/learn-string-manipulation-by-building-a-cipher/6555201d9b7fc917399f9f0b.md

latest1.4 KB
Original Source

--description--

The .isalpha() method returns True if all of the characters of the string on which it is called are letters. For example, the code below returns True:

py
'freeCodeCamp'.isalpha()
# True

Delete the condition char == ' ' and replace it by calling the .isalpha() method on char.

--hints--

You should have if char.isalpha(): in your code.

js
({ test: () => assert.match(code, /if\s+char\.isalpha\s*\(\s*\)\s*:/) })

--seed--

--seed-contents--

py
text = 'Hello Zaira!'
custom_key = 'python'

def vigenere(message, key, direction=1):
    key_index = 0
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    final_message = ''

    for char in message.lower():
--fcc-editable-region--    
        # Append space to the message
        if char == ' ':
            final_message += char
--fcc-editable-region--
        else:        
            # Find the right key character to encode/decode
            key_char = key[key_index % len(key)]
            key_index += 1

            # Define the offset and the encrypted/decrypted letter
            offset = alphabet.index(key_char)
            index = alphabet.find(char)
            new_index = (index + offset*direction) % len(alphabet)
            final_message += alphabet[new_index]
    
    return final_message
    
encryption = vigenere(text, custom_key)
print(encryption)
decryption = vigenere(encryption, custom_key, -1)
print(decryption)