A woman and her husband were standing in the Lourve observing the Mono Lisa. The husband turned to his wife and commented: "I don't see what so great about this painting." In response, the musuem guard standing nearby leaned over and replied: "Ah! But don't you wish you could!"
The ability to behold beauty, creativity, and talent is a gift. In a domain that is new to us, we may sometimes feel like the husband in this story: failing to appreciate what is in front of us.
At first, you may have a similar experience as you learn how to program. There are many ways to write a program, but some are better than others. You might not appreciate the intelligence behind the differences at first, but as you practice and study, the differences will become more clear. You will learn the patterns, intuitions, and techniques that make programming enjoyable and powerful.
Then, not only will you be able to appreciate the competent work of others, you will be able to create intelligent, beautiful code of your own.
Writing code is like painting.
There are techniques to learn, like color-mixing, brush strokes, layering, shading, perspective, etc.
There is also creative intuition: the ability to exercise the techniques you know to create something beautify.
To build beautiful code, you'll need to know the techniques, and you'll need to develop creative intuition.
Over the next few days, we'll talk about important techniques and practice, practice, practice!
Consider the following task: fill the bottom row of a bit world with green.
Step through the code, one step at a time.
Point out how the loop condition is checked before each run of the body.
Show how the loop body is skipped when the condition is
If necessary, change the last line to
bit.paint('blue') to clarify that the body is completely skipped when the condition is false.
Follow a similar approach for each of the demonstrations in this lesson.
while <condition>: if the condition was True run these lines then check the condition again
Repeating the same lines of code is called a loop.
A loop body contains the instructions we want to run multiple times.
The loop condition determines whether the loop body will be run.
The loop body is indented 4 spaces relative to the
from byubit import Bit @Bit.run_from_empty(5, 3) def go_green(bit): while bit.front_clear(): bit.paint('green') bit.move() bit.paint('blue')
A condition is something that is either true or false.
We can represent the concept of true or false in python using the values
These are booleans (named after George Boole; you can look him up). In python the type is called
Consider the following
from byubit import Bit @Bit.run_from_empty(5, 3) def go_green(bit): while bit.front_clear(): bit.paint('green') bit.move()
Why wasn't the last square painted?
from byubit import Bit @Bit.run_from_empty(5, 3) def go_green(bit): while bit.front_clear(): bit.move() bit.paint('green')
while loops, always consider the boundary conditions.
A boundary condition is the state of your program when a loop starts or finishes.
whileloop, be clear about the boundary conditions.
from byubit import Bit @Bit.run('blocked') def blocked(bit): bit.move()
front_clear(), Bit can also check
You can turn a
False and a
True using the
Point out how
not turns it into
Move Bit to the blue square.
What happens when the condition of a
while loop is always
Bit has an "infinite loop detection" feature, so if you make a mistake and create an infinite loop, it won't be too much of a hassle.
Getting stuck in an infinite loop is not problem (your computer will not explode), only an inconvenience. Even experienced programmers still run into them from time to time.
Also, not all infinite loops are mistakes. Sometimes you actually do want a loop to run forever (or close to it).
Let's paint something.
Make sure to draw this out. Demonstrate how the students can use drawing to grasp the nature of the problem and design a strategy.
Demonstrate how drawing can be used to clarify boundary conditions.
Give the students time to discuss together (in pairs) how they would break the problem up.
Demonstrate the process of going from abstract stages to concrete, technical requirements. For example, getting to the red square is an abstract stage. Deciding that you need a
while loop with an looping condition of
not bit.is_red() and a body with
bit.move() but no painting are the technical requirements.
Where will the state be after the first stage completes? Where will Bit be? What direction will he be facing?
What is the condition?
What if there were little green bushes in the way? Would
bit.is_empty() still work?
What if the base of the tree were blue? Would
not bit.is_red() still work?
There are many ways to solve a problem. If the nature of the problem changes, even a little, you will likely need to adapt your code (e.g. change
not bit.is_red()). This is a natural, essential part of the programming experience.
What is the loop condition?
Use the stepper to back off the error and observe where the problem occurred.
Observe how the
paint('red') clobbers the square and colludes with the loop condition.
Note how the loop body and condition interact. Planning out and thinking through this interaction is important.
With move-then-paint, do you move first to get off the original red square, or do you simply repaint it?
Sometimes it is more convenient to repaint a square or two. That's fine. But avoid recless repainting (i.e. inserting paint statements all over your code) as a workaround to poorly-understood boundary conditions.
Use the snapshot feature to show how the students can coordinate stages of their solution with positions in their code.
Finally, show how the code works on both problems (switch between the two tabs).