Quite often after covering a big chunk of theory, I will try to get students to think about how to actually put it into practice. This task was very successful and resulted in one student going away and spending hours trying to figure out how to make the boxes work perfectly. After a bit more theory, it is nicely reusable too in the Noughts and Crosses game (with a little bit of a tweak to print the contents of a 2D array inside the boxes.)
Knowledge needed: Loops, Conditional statements, Print statements
1. Create some code which will print a box that looks like this:
+ -- + | | + -- +
This is a useful learning exercise in itself as students will realise they must print on three separate line to make the box. It also introduced the pipe character which might be useful when they encounter it as the “or” operator in some other languages.
2. Now make some code that will use a loop to print a row of boxes.
Here is a good opportunity to use a for loop, e.g. for i in range(5) should print 5 boxes
+ -- + -- + -- + -- + -- + | | | | | | + -- + -- + -- + -- + -- +
Fairly predictably, the first thing you will get will be some code that looks a little bit like this and results in a huge mess.
for i in range(5): print "+ -- +" for i in range(5): print "| |" for i in range(5): print "+ -- +"
Why does this code not work? The print statement moves on to the next line before we are ready. I would then offer this hint: If you don’t want to move on to the next line after printing something, put a comma e.g. print “+”, This will probably result in them simply inserting a comma at the end of the print statement, which means everything is on the same line (albeit wrapped by the display.)
When do we want to move on to the next line then? Hopefully, some clever person will realise that we need to move to the next line after each loop is done, which is a fabulous place to ask “well how can we do something outside of the loop” and hammer home the beautiful elegance of proper indentation. I suppose you could move on to the next line by printing an empty string with a comma, but this is also a great opportunity to point out that if you did it like that, you would end up with this:
+ -- ++ -- + | || | + -- ++ -- +
..which is not desirable. As my University logic lecturer used to say, the solution to this is trivial. I used to loathe this expression because I usually had no idea what he had been talking about for the previous 40 minutes, so the solution is below. Please don’t hate me! 🙂
for i in range(5): print "+ --", print "+" for i in range(5): print "| ", print "|" for i in range(5): print "+ --", print "+"
3. Now see if you can use a nested loop to create a square of boxes.
This is useful to reinforce the concept of “nesting”. I usually find students think nesting is the same as indentation. We could just whack a for loop around all three of these other loops and call it a day. However this would have the same problem of duplicate tops and bottoms of boxes that we just solved in part 2 for the sides. The solution is of course to only print the top row once (use an if statement to determine whether this is the first iteration of the outer loop), and then print all of the other rows as U shapes:
| | + -- +
Again, try to have them come up with this – if there are enough people in your class (there never seem to be in mine) they could be working on this problem in pairs or groups. Quite often I find myself going too quickly over things which are really obvious to me – I have to stop myself and not panic about taking 10 minutes over something I think is easy, if it’s a really valuable thinking experience for the class.
4. Put your code into a procedure boxes(rows,cols)
Not too tricky you might think, but often tough to reconcile their understanding that the number of times the loop executes is the number of rows or columns in the box.
def boxes(rows, cols): # Define rows loop for i in range(rows): # Put previous column loops here
5. Extensions – other stuff you can reasonably ask them to do
It’s always useful to have something else up your sleeve to challenge the really adventurous ones. These are things I have asked people to do, with decent results:
- Validate the arguments to the procedure to check they are actually integers
- You may also wish to validate that the arguments are within a given range as if you call the procedure with very large arguments a runtime error will occur due to lack of memory
- Print column numbers (i.e. each row has 1, 2, 3, 4…etc. in the boxes)
- Print ascending numbers in the boxes (e.g. for a 3 by 3 box, you would get 1 2 3 in the top row, 4 5 6 in the second row and 7 8 9 in the bottom row)
(Note: both of these are not too hard, but for two or three digit numbers will utterly mess up the pretty linear fashion of the boxes! They will also annoy the students because of the loop counter starting at 0 thing…something good to get used to!)
- Print the ascending numbers in the boxes as above, without the boxes breaking and looking like a dog’s brekkie. Bow wow.
+ -- + -- + -- + | 1 | 2 | 3 | + -- + -- + -- + | 4 | 5 | 6 | + -- + -- + -- + | 7 | 8 | 9 | + -- + -- + -- +
Worksheet and the solutions
Here is the Thinking_Tasks_Boxes worksheet I give out with this lesson. It’s not the prettiest worksheet ever, but it works. I have full solutions to all of the problems including the extensions, but to avoid enterprising students (including my own!) using them you’ll need to tweet me @codeboom 🙂
Also two other things. Is it possible to get a syntax highlighter for WordPress? And also check out the “Sweet resources” section (Dude!) for my full student book for OCR AS Computing F452.