Fix tree Transcript
[00:00:00]
INSTRUCTOR: Hi there. Let’s do this activity together. Bit has a nice tree in his
yard, but it’s missing its trunk. Don’t worry about the physics. We just want to
help bit fix the tree by adding in the trunk. And as we think about a strategy
towards solving more and more complex problems, a couple of ideas to think
about.
[00:00:22]
We want to break the problem into smaller pieces. What’s the first goal?
What’s the second goal? And we want to solve the first piece before we start
the second piece. And make sure the first piece is working correctly.
Start visual description. Text on slide reads:
fix_tree.py
Bit has a nice tree in his yard, but it is missing its trunk!
Help Bit fix the tree by adding the trunk.
Happy Tree
When solving complex problems:
- Break the problem into smaller pieces
- What is the first goal? What is the second goal?
- Solve the first piece before you start the second piece. Make sure the first piece is working correctly.
- Do NOT try to solve the whole problem all at the same time.
- Don’t try to do it all in your head!
- DRAW PICTURES
End visual description.
[00:00:35]
It’s much easier to fix code, fix a problem in your code when you only have a
little bit of code. So you could write 100 lines of code, run it, find out that
something’s broken and you have 100 lines to look for.
[00:00:52]
Or you could write 10 lines of code, run it, something doesn’t work. Much
easier problem to fix. And then once that’s working and you move on from
there, the likelihood that that problem crops back up in that code that you’ve
already tested is very small.
[00:01:05]
And so it allows you to incrementally build up a solution that can be quite
large and keep it under control, make it manageable. So don’t try to solve the
whole program at the same time. Instead, try to break it into pieces.
[00:01:20]
And as you’re learning, don’t try and do it all on your head. In fact, I have
scratch paper right here on my desk that I still use as I’m coding things up in
order to sketch out how I think a program should fit together and how it
works and what are the different pieces.
[00:01:38]
It’s a fundamental part of the programming experience, not just a crutch that
you use. And so don’t try and do this all on your head. Draw some pictures,
right? So with that, let’s draw some pictures.
[00:01:53]
There’s our video from last time. Let’s start fresh and sketch this out, right? So
bit here’s bit’s yard, and we’ve got a little trunk up here, and there’s some
grass, some leaves up at the top, right, that starts over here.
[00:02:10]
Great. What are the first two steps that we see? I’d probably say, well, get to
the trunk first, and once we’re to the trunk, let’s paint in the rest of the trunk.
We get to the base, and then paint in the rest.
Start visual description. Instructor sketches a diagram to help solve the coding problem. End visual description.
[00:02:27]
So there’s two key steps here. The step of getting to the base of the trunk, the
stump, that’s just to go to red. We’ve done things like that. That’s not too
hard. And then from there, we have a paint until green.
[00:02:44]
So it’s two pieces that are familiar. We just need to put them together. So let’s
go over to some code and see what we can do here. We’ll open up PyCharm,
and open up FixedTree, clean up the formatting a little bit and let’s
implement.
[00:03:04]
So the first two goals we had were get to base of tree and then paint the
trunk. So one way to do this get to base bit and then paint the trunk. If those
words existed then this would be done and that would be easy and the nice
thing about programming is you can say well that’s the word I want and now
let’s create a function get to base to make that word exist
[00:03:42]
So in this case here we’re just trying to say move it to the red square. That’s
pretty straightforward. While not bit.red, bit.move. And now I’d pause and
say hey let’s see if this actually works.
[00:04:06]
Now right now with that red squiggly, if I try and run this fix tree, it’s going to
yell at me name paint the trunk is not defined. So it was it was trying to run
and then when it got to line 16 with the red squiggly through an error paint
the trunk has not been defined yet and it’s happening on both of our worlds.
Start visual description. Instructor types the following code:
def get_to_base(bit):
"""Move bit to the red square"""
while not bit.is_red():
bit.move()
@Bit.worlds (‘fix-tree’, ‘fix-another-tree’)
def fix_the_tree (bit):
# Get to base of tree
get_to_base (bit)
# Paint the trunk
paint the trunk (bit)
End visual description.
[00:04:31]
So we’ll want to fix that, but we can see already that we can step along
through our code and get up to that point and see that we ended up in the
right spot and then it crashed. So get to base is working and now we can
move on rest assured okay that that piece is good and do the fix the trunk.
[00:04:52]
So let’s go in therem let’s implement this. So I can say hey create that
function paint the trunk bit. I don’t have to use the shortcuts down here. I
could come up here and type def paint the trunk, etc. Shortcuts are nice
when they’re available. So here we’re going to want to say paint red until
green.
[00:05:16]
Okay, now what we haven’t included in this bit here, this portion of code, is
which direction is bit facing. So when we finished, get to trunk, bit’s right here
on that square facing over, but to paint the red he’s got to turn and look up.
[00:05:37]
So we could either put that logic, the turn, in one of those two functions, or
we can just put it in the glue code. My intuition is that we just put it right
here, bit, he’s looking this way, he’s got to turn up, bit.left.
[00:05:54]
And now paint the trunk can be just a very simple paint until green function.
Easy to keep track of. So what is that? That’s while, not bit.is,green. We’ve got
bit.move, bit.paint.red, right? Hooray!
[00:06:18]
Let’s run it. See if it works. We test it and oh we got an error. Look at this for a
moment. What just happened? Bit blasted off and blew the tree apart. Why
did that happen? As we start to step back through, so bit line 13, bit moved,
bit painted red.
[00:06:49]
And now what’s the next line it’s going to check? It’s going to check is green
on line 12 and it’s not green and so therefore it’s going to run this body again.
So he’s going to move and then he’s going to paint red.
[00:07:02]
Now we see the problem, right? We’re overriding the green squares that tell
us when to stop. And so he never stops and he just runs right up into the top
of the world. So we need to fix that. So we come back in here.
[00:07:16]
We can see now, okay, line 14, the last thing it does in the loop is paint red
and therefore this check will never quit because it will always be red when
this runs. So in this case let’s just grab bit.move and let’s cut it.
[00:07:37]
Let’s just switch the order of the lines. Let’s paint red and then move. And if
the spot we just landed on is green, then we should stop. Does that solve our
problem? Let’s take a look. Yes, yes it does, right?
Start visual description. Instructor types the following code:
def paint_the_trunk (bit):
"""Paint red until green"""
while not bit.is_green():
bit.paint(‘red’)
bit.move()
Instructor runs the code and a grid appears with the correct tree drawn on it.
End visual description.
[00:07:56]
We check fixed tree, we can switch to the other world. We see that that same
world is also correct. We’ve solved the problem. Great work. Have fun on
your own.