Python Cipher Step 38 - vague intro to IF statement
Describe the Issue
A few learners have been tripped up by this. This lesson introduces the IF statement like this:
To maintain the original spacing in the plain message, you'll require a conditional if statement. This is composed of the if keyword, a condition, and a colon :
if <condition>: <code>At the top of your for loop, replace print(char == ' ') with an if statement.
This is the first time the learner is introduced to IF and since they are replacing print(char == ' ') it can be natural to write it like this:
if (char == ' '):
Which is valid Python and works in the Preview window, that makes it even more confusing because it appears to work. However the test requires no parentheses, which is more correct:
if char == ' ':
This would also be natural to people who have already learned JavaScript if (a > 0) {, and to people who already know Python (since it's valid Python and it's easy to edit print(char) into if (char)).
I think it would help if the original explanation included an example with no parantheses:
if x > 5:
print("x is greater than five!")
Affected Page
https://www.freecodecamp.org/learn/scientific-computing-with-python/learn-string-manipulation-by-building-a-cipher/step-38
Your code
if char == ' ':
print("space!")
Expected behavior
I think it would help if the original explanation included an example with no parantheses:
if x > 5:
print("x is greater than five!")
Screenshots
System
- OS: Windows 10, Ubuntu 20.04
- Browser: [e.g. Chrome, Safari]
- Version: [e.g. 22]
Additional context
No response
It would be good to direct campers away from using parenthesis in simple conditions. Not because it's not valid Python, but because that's not a good case to use parenthesis.
It would be good to direct campers away from using parenthesis in simple conditions. Not because it's not valid Python, but because that's not a good case to use parenthesis.
Agree! I just want to include a clear example without parenthesis. It's the first introduction to the IF statement and it's a bit unclear:
if <condition>:
<code>
I'd like to include a concrete example to follow:
if x > 5:
print("x is greater than five!")
This is also in the top 3 forum post generating steps for Cipher (7 topics opened for this step since it was launched Dec 20)
A few more examples of how learners are getting confused here:
https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-38/660307/1
if (char==" "):
char==true
print(space!)
https://forum.freecodecamp.org/t/learn-string-manipulation-by-building-a-cipher-step-38/660270
if (char == "space!")
This step is still in the top 3 forum posts for Cipher, over 1 post per day.
The explanation needs to be improved for sure, but I would not add that specific example.
if x > 5: print("x is greater than five!")
We should avoid text within the code blocks because it can complicate the translation workflow.
We should avoid text within the code blocks because it can complicate the translation workflow.
Would something like this work better?
if x > 5:
print(x)
Updated my proposal for the #description:
Currently, spaces get encrypted as c. To maintain the original spacing in the plain message, you'll require a conditional if statement. This is composed of the if keyword, a condition, and a colon :.
if <condition>:
<code>
The <code> block is indented to include it in the if statement. Only if the <condition> evaluates to true then <code> is executed. In this example, if x is a value greater than 5 then x will be printed.
if x > 5:
print(x)
Take note of the output of the print statement at the top of your for loop. You can use the same comparison to check if char is equal to an empty space in an if statement. Note that if <condition> does not require parentheses.
Remove print(char == ' ') and replace it with an if statement. If char is equal to an empty space then print the string 'space!'. Remember to indent this line.
It still needs some rephrasing in my opinion, but that example is definitely better.
What really complicates it is the existing print(char == ' '). It's so easy to just modify it to if (char == ' '): I did that myself the first time I saw this step. This leads me to:
- Acknowledge that the existing
char == ' 'is the comparison we want, or totally re-explain that as the lesson currently is worded. People are getting confused by this and coding intrueandfalse - Emphasize that the existing parentheses should be removed.
8 out of 15 opened topics made this very similar mistake (I included an interesting outlier here):
if (char == "space!")
if <char == ' '>:
if (char==" "):
if(char == " "):
if <char == " ">:
if (char == " "):
if(char== ' '):
if char is empty space True otherwise False:
if(char == ' '):
Yeah, I know. We should say explicitly that parentheses are not needed.
if char == ' ':
print('space!')
The parentheses can be helpful to improve readability in certain situations, especially when combining multiple conditions or more complex expressions.