Learn String Manipulation by Building a Cipher - Step 39

Tell us what’s happening:

I am a little overwhelmed by this task. I’ve tried several of my own ideas as well as a few in the forum but I can’t solve this task without help, thank you for your help!

Now, instead of printing space! , use the addition assignment operator to add the space to the current value of encrypted_text .

Your code so far


/* User Editable Region */

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted_text = ''

for char in text.lower():
    if char ==' ':
        print('space!')
    index = alphabet.find(char)
    new_index = index + shift
    encrypted_text += alphabet[new_index]
    print('char:', char, 'encrypted text:', encrypted_text)

/* User Editable Region */

Your browser information:

User Agent ist: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 39

Hello Jonas,

Your current code has nothing changed, so I can’t see where you’re stuck.
The question is as followed:

Now, instead of printing space! , use the addition assignment operator to add the space to the current value of encrypted_text .

So what you got to do is replace the print call, and add the space to encrypted_text in it’s stead using the additional assignement operator.

As an example if I wanted to add the variable my_text to the variable old_text with the additional assignement operator I would do something like old_text += my_text.

Using this example can you solve the question?

2 Likes

This has helped quite a bit but I definitely have some research to do on how the operator works.
Is the assignment operator used in the same line as the “if function”?

You would use it in the body of the if statement.
The if statement is to figure out if the current char is a space, if it is a space we want to add it encrypted text, that’s why we do the assignment inside the body so it only gets assigned if the if statement returns true.

1 Like

After a few failed attempts to include it in the function her I am again with two of my many questions…
first of all I was wondering why the if function is not linked to the index part bc I the original index was -1 adding a ‘space!’ at this point seemed easier to me.
my second question is about my code again…
I added:

In your code, how can you tell which lines are part of the if statement?

Which lines execute when char == ' ' is True? Where does the if statement code end?

why the if function is not linked to the index part bc I the original index was -1 adding a ‘space!’ at this point seemed easier to me.

You mean something like “if index is -1 then char is not present in alphabet then it must be a space, so add a space”. You might be able to do it this way. It’s a bit more complicated logically than “if char is space, add a space”.

This might not fit with something that happens later in this project. Could be a valid approach though.

1 Like

As you can see I tried indenting the if statement to line out what is a part of said statement. The code runs again but I still didn’t manage to either complete the task nor to extend the space to the ‘encrypted text’.
As the code is running again I assume that I managed to end the if statement but I still wonder where I wen’t wrong and if I even understood the task I was given correctly…
Thanks for bearing with me and helping me through my first efforts of learning to code!

Looking good, but I think it is asking you to assign the space stored in the char variable.

I would also just add spaces around your +=:
encrypted_text +=

If you don’t mind could you try to explain my problem in layman’s terms? As I said this is my first experience in computer science and I am struggling with the terminology . I thought the current value of ‘encrypted_text’ was ’ ’ and I noticed that in my earlier statement there was a space missing as I wrote encrypted text = ‘’ and not ’ '. I fixed that but I am still helpless in terms of figuring out if my problem lies in my code or if I simply misunderstood the task.
I thought I was trying to pass on the ‘space’ from char to encrypted_text. Therefore I thought that if char = ’ ’ then encrypted text = ’ '. As I am writing this I realized that I am not even sure why I wrote e_c+= ’ ’ instead of e_c = ’ '.
I hope you can understand my problem and my questions.
Best regards, Jonas

This is fine, and technically could be correct.

However, we are passing the value of char along to the encrypted_text variable. In this case, we know it’s space ’ ', but generally, it could be any value. Think of how you would generalize passing the value of one variable to another.

+=

We are in the process of breaking down a sentence into single letters, changing that letter by sliding down the alphabet 3 letters, and then rebuilding the sentence into encrypted gibberish.

To add on to a string letter by letter, you would use +=

variable = 'H'
variable += 'e' #He
variable += 'l' #Hel
variable += 'lo' #Hello
variable = 'World' #World

variable = destroys or overwrites the previous value.
variable += adds to the previous value

2 Likes

Thank you, this has been incredibly helpful in terms of understanding how to use the += operator. After reading the assignment again I finally understood the very easy task I was given to simply add the value of char to encrypted text… Thank you so much for your patience!

No problem!

If there’s some concept that you’re not fully understanding it never hurts to google it either. It’s hard to be explain fully in a concise lesson and googling syntax is somethign professional programmers do all the time, so it’s a good habit.

1 Like