Just started with Python 3: String Methods: Coded_Correspondence project. Oh my gosh I so admire those of you who are able to do this project without referring to the solutions.
I can’t even fully understand the solutions.
Please help me understand my following two questions? Link to the project below …
https://www.codecademy.com/courses/learn-python-3/informationals/python3-coded-communication
In layman’s terms, please explain to me why my code below (circled in blue) outputs a bunch of “j” letters instead of whitespace…
In more layman’s terms, why does my next blue-circled code not work. I thought we were supposed to shift all non-punctuated letters over by 10 letters. So that’s what I coded, which is exactly the same as what was coded correctly in the last line of the “if not” statement. The solution coded simply … message_decoded += letter … but to me, this looks like not moving the letter over by 10 letters which we’re supposed to do!
letter not in punctuation
is not the same as
letter != punctuation
letter not in punctuation
is False
when letter
is contained within punctuation
, meaning letter
is a substring of punctuation
, possible the same as punctuation
letter != punctuation
is False
when letter
and punctuation
are exactly the same, meaning letter
would have to be ".!? "
to make letter != punctuation
be False
Notice that
alphabet.find(letter)
gives you -1
when letter
is not found in alphabet
and when you add 10,
you get 9;
alphabet[9]
happens to be "j"
You don’t need to use alphabet
at all for a letter/character that is in punctuation
, you could just use the original letter/character.
as in
else:
message_decoded += letter
1 Like
Thank you Janbazant for your reply.
-
What do you mean by “letter is a substring of punctuation”?
-
Ah. Thank you for pointing out to me that letter != punctuation takes in ".!? " as a unit. I see where I went wrong there.
-
I don’t understand “alphabet.find(letter) gives you -1 when letter is not found in alphabet”. Is that just a concept that I missed in the lesson? And just to clarify, when letter is not found, you mean when the loop lands on whitespace?
-
Oh I get why the else statement is simply add the letter (which is whitespace) to the message_decoded string. Thank you for explaining that to me.
for the string "abcde"
,
"abc"
is a substring, and "bc"
, and "cde"
are all substrings of "abcde"
,
but "ade"
is not a substring of "abcde"
str1 = "abcde"
print( str1.find("c") ) # this would be 2, the first index at which "c" is found
print( str1.find("m") ) # this would be -1, since "m" is not a substring
Here’s a link to that in the codecademy docs: python strings .find() method
1 Like
Thank you for your clear explanation and for referring me to the Codecademy docs link on strings.find() method!
Hi again. Moving on to Step 3. I understand everything about the code solution except the placement of the "message_decoded = “” " …
The screenshot above is of the correct placement. But I had originally put it where the blue arrow points to, at the same no-indent level as alphabet and punctuation. I am not fully understanding why my original placement in global is not correct, and why inside the local loop placement is good.
You want to return a string
so you need to declare a variable that’s a string
that you’ll add the letters to
in the function.
1 Like
When we define a function, is it always paired with a return command inside the function?
functions don’t always contain return.
(A function may change something in an object instead of returning something, for example.)
1 Like
Ok I will keep an eye out for this (when function changes something in an object) and remember that functions don’t always need to contain a return. Thank you.