1 00:00:08,070 --> 00:00:16,765 You can reduce your headaches as a programmer by writing your programs incrementally. 2 00:00:16,765 --> 00:00:21,190 "Get something working and keep it working;" that's going to be our mantra. 3 00:00:21,190 --> 00:00:24,200 Or "Start small and keep it working." 4 00:00:24,270 --> 00:00:30,380 Here's a program where we're trying to draw a house. 5 00:00:30,380 --> 00:00:33,545 Rectangle with a triangle on the top, 6 00:00:33,545 --> 00:00:38,335 and we've done the first part for you. Get something working. 7 00:00:38,335 --> 00:00:42,995 But I'm now going to try to add to it in a way that says, 8 00:00:42,995 --> 00:00:46,825 draw more and more of the house and when we make mistakes 9 00:00:46,825 --> 00:00:51,055 to catch them before there's too much code. 10 00:00:51,055 --> 00:00:54,565 So we've started with something that is working. 11 00:00:54,565 --> 00:00:58,510 Bob is going down and turning left. 12 00:00:58,510 --> 00:01:01,165 Let's complete that square. 13 00:01:01,165 --> 00:01:04,255 That should be pretty easy to do. 14 00:01:04,255 --> 00:01:10,760 Just make him turn left again, and go forward. 15 00:01:11,060 --> 00:01:14,590 You might be tempted to do more lines of code than that, 16 00:01:14,590 --> 00:01:21,595 but I really recommend testing after you write every little bit of code. 17 00:01:21,595 --> 00:01:24,800 So let's turn him again, 18 00:01:32,910 --> 00:01:35,560 and we'll test that, 19 00:01:35,560 --> 00:01:39,410 and we're going to get to a slightly harder part. 20 00:01:42,780 --> 00:01:51,170 Now, I want to make him turn this way so that he can finish the house like that. 21 00:01:52,320 --> 00:01:56,660 So, let me do that. 22 00:01:57,930 --> 00:02:01,075 Instead of turning left, 23 00:02:01,075 --> 00:02:04,570 I'm going to have him turn to the right. 24 00:02:04,570 --> 00:02:07,060 And instead of 90 degrees, 25 00:02:07,060 --> 00:02:09,745 let's just try going 45 degrees. 26 00:02:09,745 --> 00:02:17,170 We'll go halfway and then we'll have him go forward by 50 again. 27 00:02:25,430 --> 00:02:31,310 Oops! This is why it's a good idea to do things a little bit at 28 00:02:31,310 --> 00:02:38,190 a time because I'm going to get to realize already that I am going the wrong direction. 29 00:02:39,950 --> 00:02:48,430 So I wanted him to turn to here so that he would go that way but he didn't go far enough. 30 00:02:48,430 --> 00:02:51,860 He turned 45 degrees but I really needed him to 31 00:02:51,860 --> 00:02:57,150 go all the way around there, so another 90. 32 00:02:57,820 --> 00:03:06,270 So I need him to go a 135 degrees. 33 00:03:15,640 --> 00:03:18,395 That looks a lot better. 34 00:03:18,395 --> 00:03:24,365 Now I'll just have him turn 90 degrees, 35 00:03:24,365 --> 00:03:30,600 go forward, and we will have our house. 36 00:03:35,440 --> 00:03:42,035 Oh no. We don't get a house that way. 37 00:03:42,035 --> 00:03:49,110 So it turns out that we need to make him not go quite as far. 38 00:03:51,380 --> 00:03:53,610 So instead of going 50, 39 00:03:53,610 --> 00:03:56,330 he's got to go a little bit less. 40 00:03:56,330 --> 00:04:02,190 Maybe he should go 40 each time. 41 00:04:11,650 --> 00:04:14,825 Almost but not quite. 42 00:04:14,825 --> 00:04:21,420 You can try just doing trial and error until you get something that's pretty close, 43 00:04:21,420 --> 00:04:25,770 or sometimes it really helps to step back and do a calculation. 44 00:04:25,770 --> 00:04:29,960 In this case, the Pythagorean theorem, 45 00:04:29,960 --> 00:04:32,790 remember that, can really help us. 46 00:04:32,790 --> 00:04:36,230 We want to make something that is 50 47 00:04:36,230 --> 00:04:45,530 here and is going to be a right triangle there. 48 00:04:45,530 --> 00:04:50,060 If we have some length a, 49 00:04:50,060 --> 00:04:59,650 we need a squared plus a squared to equal 50 squared. 50 00:05:02,680 --> 00:05:06,470 You could do this in a calculator or you could 51 00:05:06,470 --> 00:05:10,295 have the Python interpreter work it out for you. 52 00:05:10,295 --> 00:05:11,900 There is a square root function. 53 00:05:11,900 --> 00:05:15,135 It's a little tricky, 54 00:05:15,135 --> 00:05:22,490 you have to import the math module and then we're going to say, 55 00:05:23,730 --> 00:05:34,720 distance is the square root of 50 times 56 00:05:34,720 --> 00:05:38,180 50 divided by 2 57 00:05:38,870 --> 00:05:45,970 and then we will have bob go forward by that distance each time. 58 00:05:49,550 --> 00:05:55,550 Let's try it and see if we get a nice roof on the house this time. 59 00:05:57,560 --> 00:06:01,505 Yay. 60 00:06:01,505 --> 00:06:06,775 So, the main point that I wanted you to take away from this was 61 00:06:06,775 --> 00:06:10,510 that you're unlikely to get it all 62 00:06:10,510 --> 00:06:14,260 right the first time if you just write a really long program. 63 00:06:14,260 --> 00:06:18,175 It's much better to start with something that's working at a line or two, 64 00:06:18,175 --> 00:06:24,490 test it and make sure that you continue to have a program that is working. 65 00:06:24,490 --> 00:06:29,290 Build up and always keep your program working.