Learn String Manipulation by Building a Cipher - Step 66

Tell us what’s happening:

The instructions are to

Call your function passing text and custom_key as the arguments. Store the return value of the function call in a variable called encryption.

I don’t understand what I’m supposed to do to make this work. I’ve tried everything and I get nothing but errors. I’m totally stumped and I’ve been on this problem for a couple days now.
vigenere(text, custom_key) is calling the function right?
why is return not working?
isnt setting the variable for encryption correct too?

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

# User Editable Region

vigenere(text, custom_key)
    return vigenere()
encryption = vigenere()

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 66

Welcome to the forum @ZeroC00l

  1. Call your function passing text and custom_key as the arguments. ✓
  2. Store the return value of the function call in a variable called encryption . ✗
1 Like

Yes :+1:

You already end your function with a return statement. That’s how you exit the function. You don’t need to write return again.

Adding to what @Teller has written, because you’ve been on this for so long:

You’ve kind of spread the answer out on multiple lines, it’s a one line solution:

variable = function(arguments)

Nothing else.

1 Like

I think I understand. Because I already put a return in the function by way of the indentation, whenever I use vigenere() again that return is included.

I see what the solution is and it makes sense reading, but I think overall im still unclear as to what “return” does. Not necessarily in this specific case but in general and how is it different than “print”?

Yes, exactly

return defines what is returned when calling a function. You can think of it as the function running and the return value replacing the function call in the code.

def add(arg1, arg2):
    return arg1 + arg2

print(add(5, 1))

You can imagine the call add(5, 1) here executing and then being replaced by 6:

def add(arg1, arg2):
    return arg1 + arg2

print(6)

Then print sends it to the console. This would print nothing:

add(5,1)

6 would be returned but go nowhere it would just disappear in memory somewhere.

If you did not have return in your function:

def add(arg1, arg2):
    result = arg1 + arg2

print(add(5, 1))

Nothing is returned, nothing is printed. It resolves to this:

def add(arg1, arg2):
    result = arg1 + arg2

print()

You can read more here if that wasn’t clear:

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

https://www.geeksforgeeks.org/python-return-statement/