why/how is it that a variable can be assigned a value that is the same as the variable?
Whats the point?
(Line 8)
I donāt understand what you mean with āa value that is the same as the variableā, what do you mean?
oh, you mean the encrypted_text = encrypted_text +
? you want to keep the old value of encrypted_text
and add something new to it, not discard the old value and assign a completely new value. You can use encrypted_text +=
as a shortcut for that, this is called compound assignment
Im still a little confused. The previous value of encrypted_text was blank (encrypted_text=āā)
so what was the previous value?
Even if itās blank you can still add to it.
In this case it will start off empty, the first character will be added, and then each subsequent character will be added to the previous.
The whole exercise is to break down a string into characters, shift them, then rebuild a new string character by character. So you keep adding to the final string one letter at a time.
Sorry for asking so many questions:
So leaving the value empty, in this case, is due to the variable pretty much waiting for something to pass through it? And everything else was us setting up for this?
Yes, exactly!
Technically you donāt need to declare the variable ahead of time, it will be created when you assign it in the loop, but itās good practice to declare all of your variables at the top, so you can see what youāre working with and plan your program.
In this case, itās declared and assigned an empty string, so we know we have a string variable to work with.
Ask as many questions as you can think of!
Just to be clear, here itās not a matter of good practice. If you donāt declare the variable before, you got a NameError
when you try to do something like:
variable = variable + something_else
.
Right, because the variable is on the other side of the operator here, thanks for pointing that out
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.