You can reduce your headaches as a programmer by writing your programs incrementally.
"Get something working and keep it working;" that's going to be our mantra.
Or "Start small and keep it working."
Here's a program where we're trying to draw a house.
Rectangle with a triangle on the top,
and we've done the first part for you. Get something working.
But I'm now going to try to add to it in a way that says,
draw more and more of the house and when we make mistakes
to catch them before there's too much code.
So we've started with something that is working.
Bob is going down and turning left.
Let's complete that square.
That should be pretty easy to do.
Just make him turn left again, and go forward.
You might be tempted to do more lines of code than that,
but I really recommend testing after you write every little bit of code.
So let's turn him again,
and we'll test that,
and we're going to get to a slightly harder part.
Now, I want to make him turn this way so that he can finish the house like that.
So, let me do that.
Instead of turning left,
I'm going to have him turn to the right.
And instead of 90 degrees,
let's just try going 45 degrees.
We'll go halfway and then we'll have him go forward by 50 again.
Oops! This is why it's a good idea to do things a little bit at
a time because I'm going to get to realize already that I am going the wrong direction.
So I wanted him to turn to here so that he would go that way but he didn't go far enough.
He turned 45 degrees but I really needed him to
go all the way around there, so another 90.
So I need him to go a 135 degrees.
That looks a lot better.
Now I'll just have him turn 90 degrees,
go forward, and we will have our house.
Oh no. We don't get a house that way.
So it turns out that we need to make him not go quite as far.
So instead of going 50,
he's got to go a little bit less.
Maybe he should go 40 each time.
Almost but not quite.
You can try just doing trial and error until you get something that's pretty close,
or sometimes it really helps to step back and do a calculation.
In this case, the Pythagorean theorem,
remember that, can really help us.
We want to make something that is 50
here and is going to be a right triangle there.
If we have some length a,
we need a squared plus a squared to equal 50 squared.
You could do this in a calculator or you could
have the Python interpreter work it out for you.
There is a square root function.
It's a little tricky,
you have to import the math module and then we're going to say,
distance is the square root of 50 times
50 divided by 2
and then we will have bob go forward by that distance each time.
Let's try it and see if we get a nice roof on the house this time.
Yay.
So, the main point that I wanted you to take away from this was
that you're unlikely to get it all
right the first time if you just write a really long program.
It's much better to start with something that's working at a line or two,
test it and make sure that you continue to have a program that is working.
Build up and always keep your program working.