Learn String Manipulation by Building a Cipher - Step 76

Tell us what’s happening:

It’s really the last part of this exercise that confuses me. What does it mean to check if the if statement is alphabetic. I guessed that it would be
if char.isalpha( ) != alphabet but that came back wrong.

Your code so far

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

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

    for char in message.lower():

/* User Editable Region */

        # Append space to the message
        if char.isalpha() != char:
            final_message += char

/* User 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)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 76

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

Hi @legacyofl2017 ,

if char.isalpha( ) != alphabet this condition will not work as intended because char.isalpha() returns True for any alphabetic character.
However, since our goal is to check if the character is not alphabetic if not char.isalpha(): will check if the character is not alphabetic based on the built-in isalpha() method. Please let us know if this was helpful.

,

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.