Common Struggles

Tags: python teaching education

Over the past few years, I’ve taught hundreds of kids to code using Python in workshops, after school programs, and as part of school curriculum. Most of the students I teach are around middle school age (11-14). I find this age to be a great time to start learning. The kids are very engaged and capable - and very motivated to learn, if given the chance.

I prefer Python for teaching kids for a number of reasons, foremost because it’s a very beginner-friendly language. That said, however, there are still a number of very common areas where kids tend to struggle the most.

Below is a list of the problems I encounter most often in the classroom:

Nested parentheses

One of the most common syntax errors I see among students is leaving off the ending parentheses when one statement is nested inside another. Nested parentheses happen frequently, and help with both readability and reducing code complexity.

For example, instead of doing this:

angle = randrange(0, 360)
turtle.left(angle)

We can simplify by nesting the randrange() call inside the parentheses of the left() function:

turtle.left(randrange(0, 360))

Often, the first time they see something like this, the question from my students will be “Why are there two parentheses at the end?” It’s very important to emphasize that parentheses come in pairs and there should never be an odd one out. Sometimes, I’ll encourage students to use spaces to highlight the nesting (although PEP8 fanatics will cringe):

turtle.left( randrange(0, 360) )

Missing colons on loops and functions

One of the first constructions students learn is the for loop, which allows you to repeat a block of code multiple times:

for i in range(5):
    print(i)

That colon at the end of the first line is very important, because it indicates that the indented code coming after it is the block that belongs to the loop (ie the code that is to be repeated).

Forgetting to type this colon has a couple of implications. In addition to resulting in an “expected an indented block” error message, it also means that the editor (usually IDLE when working with beginners) won’t automatically indent the next line - so now the student has two problems: adding the colon and fixing the indenting.

Indentation

As shown in the previous example, indentation is important to Python. It indicates code blocks and how they are related. IDLE actually does a pretty good job of trying to automatically indent, but it only goes in the forward direction - if you need to unindent one level, you have to hit “Backspace.” This leads to problems like the following, which will result in an “syntax error” message:

if guess > secret:
    print("Too high!")
    elif guess < secret:
        print("Too low!")
        else:
            print("You got it!")

The correct indentation:

if guess > secret:
    print("Too high!")
elif guess < secret:
    print("Too low!")
else:
    print("You got it!")

IDLE issues

IDLE is the code editor and development environment that is installed automatically when Python is installed on your computer. It’s a great tool for beginners, because it allows them to get started right away without needing to install any additional, complex software development tools. However, it also has a number of annoying quirks that can waste huge amounts of a beginning coder’s time.

This is the single biggest time waster and source of frustration. When you launch IDLE, you first see a shell window. If you want to start typing a new program, that means opening another window (which will be the same size, so now they’re overlapping). When you run your program, the output shows up in the shell window (if it’s text-based) or in yet another window (if it’s graphical). Kids are constantly forced to struggle with moving overlapping windows around to find the one they need. Often, they’ll close the Shell window, thinking they don’t need it, only to find that it being automatically reopened the next time they run their program. To top it all off, IDLE doesn’t even try to remember window positions, so even if you do get things arranged in some sort of useful fashion, the next time you open a new window it all begins again.

I showed this to a usability expert once and she thought it was a nightmare. I am constantly amazed at how patient and accepting kids are of these problems. Often, I am more frustrated as the instructor than the kids are with the whole process.

When you’ve typed your code and it’s time to try running it, you need to click on the “Run” menu. This is fine, but there are three options in this menu, and it’s impossible for a beginner to guess which is the right one. There’s “Python Shell” (which we already have popping up all the time anyway), “Check Module”, and “Run Module.” The correct choice is “Run Module”, which just leads me to teaching them to use the “F5” shortcut as soon as possible.

Errors can be quite confusing in IDLE. Sometimes they generate popups (ex: “invalid syntax” or “expected an indented block”) while others show up in the Python Shell window. The popups are especially troublesome, because they’re modal (meaning you must click “Ok” on them before you can do anything else) but not always on top. So what happens is a student will see the message and then click on their code window to see what’s happening. Now there’s a tiny modal window hiding where it can’t be seen and the student is unable to type and thinks their computer has crashed.

This one is mainly a problem for me as a teacher. There are only a limited number of font size options (with 22 being the largest). When I’m displaying code on the projector, it can sometimes be hard to read from across the room, or I’d like to zoom in on a particular chunk of code. In any other editor on the market, you can hit a +/- key combination to zoom the font size as big or small as you like, but in IDLE you are stuck with limited settings.

A note on typing

All the above notwithstanding, I must point out here that the single most important factor in my experience that determines how much a student struggles with the learning to code is typing ability. A student who is spending time and mental energy on hunting for the right keys is taking attention away from understanding the difficult concepts of the lesson. In addition, they’re slower than other students so they start to feel pressure to keep up, which leads to a frustrating experience all around.

There are many great free typing practice websites to choose from. My favorite is Typing.com. Encourage your kids to practice - they’ll thank you later!

Comments