Learn String Manipulation by Building a Cipher - Step 49

Tell us what’s happening: not sure why my code is being thrown,

renamed text = message and shift = offset inside
inside of the function

Describe your issue in detail here.

Your code so far


/* User Editable Region */

text = 'Hello Zaira'
shift = 3

def caesar(message, offset):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    encrypted_text = ''
    text = message
    shift = offset
    for char in text.lower():
        if char == ' ':
            encrypted_text += char
        else:
            index = alphabet.find(char)
            new_index = (index + shift) % len(alphabet)
            encrypted_text += alphabet[new_index]
    print('plain text:', text)
    print('encrypted text:', encrypted_text)

caesar()

/* 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 49

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.

Hello Thomas,

You’re not supposed to re-assign the variables, you have to replace them, only in the function body to match the function signature. So instead of text = message you would literally turn all mentions of text to message, the same goes for shift and offset.

1 Like

ok i got it to work with how you said to do it,
do you know why i cannot reassign the variables in this instance? i also dont understand the concept of why we put message and offset in the function
Caesar (message, offset)

1 Like

Yes of course!

So let’s just quickly only look at the important parts.

text = 'Hello Zaira'
shift = 3

def caesar(message, offset):
    # code to encrypt

I skipped everything below it, but obviously will pretend the function is still working to encrypt a message.

If you call the function you would do so by using:
caesar('Hello Zaira', 3)

Which would then use these inputs to encrypt Hello Zairy with an offset of 3. Pretty cool. But you may not always want to use literal text or maybe you want to try the same text with different shifts. To just go ahead and write:

caesar('Hello Zaira', 3)
caesar('Hello Zaira', 5)
caesar('Hello Zaira', 7)

Would start getting tiresome at some point. (And is also not as neat).
This is where the variables of text and shift comes in. we can just call the function with those inputs like so: caesar(text, shift). Way neater, and if we wanted to try multiple shifts it was be slightly nicer:

caesar(text, shift)
caesar(text, 5) #
caesar(text, 7)

That’s cool and all, but now imagine the code again, but the variables, and also the parameters in the code are the same. and we’re just gonna print the arguements.

text = 'Hello Zaira'
shift = 3

def caesar(text , shift ):
    print(text, shift)

caesar(text, shift)

What would be printed in this case? It would get very confusing quickly.

So to keep it short in the initial code, text and shift are your inputs. And message and offset is what you use to make your code do things (possibly the output).
Your input can be anything, it doesn’t have to be text and shift but what’s in the body of your function won’t change.

2 Likes

thank you so much, I have been scrolling and checking every question about this, even though I already tried this method, but thank you! it works now

1 Like