swcarpentry / python-novice-inflammation

Programming with Python
http://swcarpentry.github.io/python-novice-inflammation/
Other
300 stars 778 forks source link

Missed opportunity to talk about indentation in "Programming with Python : Repeating actions with loops" #687

Open datasolver opened 5 years ago

datasolver commented 5 years ago

One of the key problems I had when I started programming (in python) was with indentation especially with "for loops" and "if statements". I believe most beginners will have the same or similar issues. Therefore, I think this lesson (Programming with Python : Repeating actions with loops) is a golden opportunity to let learners have a feel of what problems they can run into without proper indentation. For example, how will the intended answer be affected if:

This code:

length = 0
for vowel in 'aeiou':
    length = length + 1
print('There are', length, 'vowels')

was written as:

length = 0
for vowel in 'aeiou':
    length = length + 1
    print('There are', length, 'vowels')
maxim-belkin commented 5 years ago

Thanks, Jamiu! You're right and, in fact, you hit a very important issue that we've got in our episode on for loops: indentation is not emphasized enough. The word indent occurs only twice in the episode: once in the body of the lesson, and once in keypoints. I suggest to discuss how to go about this and then... submit pull request(s)!

HallidayDF commented 1 year ago

I think there's a perfect opportunity to discuss it in the segment after "What's in a name". The lesson already calls for following the loop step by step to see how indented code inside the loop changes the variable defined outside the loop, I think one extra example to demonstrate how indenting the last line changes the output would fit in nicely. Potentially even squeezing in a 3rd example to demonstrate that users need to be consistent with their indentation?