Learn String Manipulation by Building a Cipher - Step 65

please correct my coding

text = 'Hello Zaira'
custom_key = 'python'

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

    for char in message.lower():
        # Append space to the message
        if char == ' ':
            result_text += char
        else:        
            # Find the right key character to encode/decode
            key_char = key[key_index % len(key)]
            key_index += 1
            
            # Define the offset and the resulting letter
            offset = alphabet.index(key_char)
            
            index = alphabet.find(char)
            if direction == 'encrypt':
                new_index = (index + offset) % len(alphabet)
            elif direction == 'decrypt':
                new_index = (index - offset) % len(alphabet)
            else:
                raise ValueError("Invalid direction. Please use 'encrypt' or 'decrypt'.")
            
            result_text += alphabet[new_index]

    return result_text


encryption = vigenere(text, custom_key, 'encrypt')
print('Plain text:', text)
print('Encrypted text:', encryption)


# decryption = vigenere(encryption, custom_key, 'decrypt')
# print('Decrypted text:', decryption)

reset the step and do only what directed:

Add a third parameter called direction to your function definition.

You don’t have to do anything else than that

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