Learn String Manipulation by Building a Cipher - Step 68

Tell us what’s happening:

im not sure of how to make my encryption variable.

Your code so far

text = 'Hello Zaira'
custom_key = 'python'

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

    for char in message.lower():
    
        # Append space to the message
        if char == ' ':
            encrypted_text += char
        else:        
            # Find the right key character to encode
            key_char = key[key_index % len(key)]
            key_index += 1

            # Define the offset and the encrypted letter
            offset = alphabet.index(key_char)
            index = alphabet.find(char)
            new_index = (index + offset) % len(alphabet)
            encrypted_text += alphabet[new_index]
    
    return encrypted_text
    encryption = vigenere(text, custom_key)

# User Editable Region



# 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/122.0.0.0 Safari/537.36

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 68

Anything that is indented like this will be part of the function.

However, you don’t want to call your function from inside the function right? Even if you did it’s right after return which means it will never get executed.

so then my call should come from before the return?

Please review how to make and call functions. This is something that you need a solid understanding of.

https://www.w3schools.com/python/python_functions.asp

https://www.geeksforgeeks.org/python-functions/

1 Like

Here’s a simple function and how to call it:

def my_function():
  print("Hello from a function")

my_function()

If I change it like this:

def my_function():
  print("Hello from a function")
  my_function()

Now I’m calling the function from inside the function. That’s fine, but when will the function be called? It’s never called if there’s no call from outside the function. It’s only defined.

To call it, you need some code outside the function that will be executed.

1. def my_function():
    print("Hello from a function")
  3.my_function()

2. my_function()

Now the program will 1. define the function then 2. call the function and then 3. call the function again, recursively forever in a loop

1 Like