Tag: cs1’
While loops gone wild
- by Hélène Martin
I hope that my students know that I too feel programming is hard. In fact, I hope some of them realize that programming is exciting and meaningful to me in large part because it’s such a challenge. When students complain about difficulty, I like to remind them that they’re in school to stretch themselves but sadly that
doesn’t tend to resonate much with them. Go figure.
In Creative Computing, we recently spent time working with indefinite loops. I think most introductory programming instructors would be shocked and appalled at how long we’ve spent primarily focusing on one
measly construct. Indeed, while loops are a half-hour lecture in most college-level intro to programming courses. But here’s the deal – while loops are hard. What does it take to use a while loop properly? I’ve tried to think of all the little things necessary to know how to interpret and create code using indefinite loops but I’m sure I’ve forgotten bits and pieces! I poked around a little for formal articles with these sorts of breakdowns for various programming constructs but wasn’t very successful. Pointers?
Understanding while loops requires…
- syntactic knowledge
- start the statement with while, have some condition in parentheses, end the line with a colon then indent things that need to be repeated
- valid boolean operators (assignment = vs. equality ==)
- creating complex boolean expressions with and and or (for example, my students often write things like value == 2 or 3 which won’t work as expected in Python)
- how to group statements into a block
- understanding of scope rules
- if I use a variable in my test, it has to have been initialized
- if I create a variable in my loop, it won’t be available later
- understanding of flow of control
- if I make a method call in my loop body, that method is run then Python keeps executing statements after the call
- if my condition stops being true somewhere in the loop body, that doesn’t make me magically jump out
- the test is executed every time around the loop
- converting between stopping conditions and continuing conditions
- DeMorgan’s Law is hard
I have a lot of sympathy for students who take more time to synthesize all of this and I think it’s too bad that we don’t have more best practices on breaking concepts down into digestible pieces. At the same time, I think CS courses are exciting for high-achieving students BECAUSE we haven’t figured out how to break things down into bite-sized chunks, yet! Certainly, when I was starting my perpetual quest to learn to program, I found something deeply satisfying about looking at several examples of a construct to establish a model of how it works. For students who are not used to considering an idea from different angles in an attempt to really understand it, it’s a deeply frustrating process.
I feel like this points to a deep need for differentiated instruction to keep my top end students engaged without overwhelming my bottom end. I’ve been trying to be more careful about giving students sample code that address sub-concepts one at a time and having different milestones so students at various levels can be successful. Again, not really finding best practices but that’s probably because computing education is primarily done at the college level where it’s not much of a concern for instructors (or I don’t know where to look?). Raymond Lister and John Leaney’s First Year Programming: Let All the Flowers Bloom does present some nice work supporting the idea of shifting my goals for weaker students to reading and interpreting code rather than producing entirely novel programs.
Of course, I don’t pretend to know what the right solution is. One thing I think is underscored by the overwhelming list of concepts needed to understand the humble while loop is the importance of being very deliberate about the order topics are introduced in. Sometimes I look at introductory programming course syllabi and I feel like concepts are just being thrown haphazardly into a pile instead of aiming to construct knowledge. Strong students will probably be fine but what about normal people?!
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 |
|
|
|
|
|
|
|
|