Conditionals
- by Hélène Martin
Obviously, as I dive into high school teaching, I’ve been thinking a lot about mental models necessary for programming and about how to form these. With 120 students of my own that I have had the opportunity to see through their very first line of code, their first loops, their first conditionals, etc, it’s become painfully clear that some students are much better prepared to think computationally than others. This is old news to most people, but it’s just a much more intimate reality for me now. I have no idea why, really… it’s not obviously correlated to their other grades or to their temperament. It’s certainly not only the basement nerds or even the particularly interested — I have an superstar freshman girl in my AP class who I just learned ended up in my class by accident!
In my exams, I try hard to uncover what students’ current mental models are and have an opportunity to review them as necessary. Although I know it’s unpopular among many, I have them write code on paper as well as solve mechanical code reading problems (Stuart is my hero and I steal all my ideas from him). Reading their solutions is so eye-opening for me, especially since I can correlate with what I’ve seen them do at the computer.
I recently had my Creative Computing students take a Python quiz. I asked them a fairly simple conditional problem to get them to define a function that takes parameters and check their conditional syntax. Something interesting came out. Here’s the problem:
Write a function choose_todo that takes two parameters: a temperature and a mood. It should print what to do according to the following rules:
|
Temperature |
Mood |
Activity |
|
60 or above |
Happy |
Go hiking |
|
60 or above |
Sad |
Go fishing |
|
Below 60 |
Happy |
Go hiking |
|
Below 60 |
Sad |
Stay home |
Ok, not particularly demanding or intellectually stimulating but the average and good students did this:
def choose_todo(temp, mood):
if(temperature >= 60 and mood == "happy"):
print "Go hiking"
if(temperature >= 60 and mood == "sad"):
print "Go fishing"
if(temperature < 60 and mood == "happy"):
print "Go hiking"
if(temperature < 60 and mood == "sad"):
print "Stay home"
And from EVERY exceptional student, we have:
def choose_todo(temp, mood):
if(mood == "happy"):
print "Go hiking"
else:
if(temp >= 60):
print "Go fishing"
else:
print "Stay home"
I had the two happy cases be the same to see if they would recognize it, but I didn’t expect it to be such a black and white thing. I pretty much could give this one problem and assign grades for the year.
The strong students somehow turned the problem around in their brain a bit more before starting to write than the others did. Of course, the first solution isn’t wrong, it’s just… less satisfying?
This is not by any stretch of the imagination a skill that I explicitly taught. I showed examples of concise code and discussed the advantages of making mutually exclusive options obviously so by favoring elif/else constructs over sequential ifs… but it was parenthetical at best. Why do some people just ‘get it?’
|
Temperature |
Mood |
Activity |
|
|
|
|
|
|
|
|