Learn String Manipulation by Building a Cipher - Step 81

Tell us what’s happening:

Describe your issue in detail here.

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():

        # Append any non-letter character to the message
        if not char.isalpha():
            final_message += 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 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

# User Editable Region

def encrypt(message, key):
    return vigenere(message, key)
    
def decrypt(message, key):
    return vigenere(message, key, -1)
    
encryption = encrypt(message,key)
print(encryption)
decryption = decrypt(message,key)
print(decryption)

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 81

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.

I am changing the cncrytion and decrytion to encrypt and decrypt but its not helping , am i not understanding

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.

There are different kinds of variables here, Variables, Arguments and Parameters.

Variables:

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

Here you set up two variables containing strings of text. These are global variables.

def encrypt(message, key):
    return vigenere(message, key)

Here you set up a function with 2 parameters. These parameters message and key become variables that are accessible only within the function. They do not exist outside the function, or are at least inaccessible.

encryption = encrypt(message,key)

You cannot send message to the encrypt function, since it does not exist outside the function. Look at the original code again:

encryption = vigenere(text, custom_key)

It is passing the variables text and custom_key to the function as arguments. When they arrive at the function they will become the parameters message and key. So, don’t change the arguments here.

text = 'Hello Zaira!' #`text` is a plain sentence, not encrypted.
encryption = vigenere(text, custom_key) #text is sent to the function and is encrypted
decryption = vigenere(encryption, custom_key, -1) #the encrypted message is sent to the decryption function

You would not send the same variable to encryption and decryption.

TLDR:
Only change the name of the function being called. Don’t change the variables being sent as arguments). Watch out that decrypt only takes 2 arguments.

1 Like

Okey Thanks for your contribution

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