CODE.ORG - COURSE D - LESSON 9 - ANSWER KEY
Computer Science
Okay, this is the answer key for lesson number 9 while loops and farmer. One point I want to point out before we get started is that this is for learning. This is not for copying. So do not skip ahead and try to just copy the answer over from what I give you. Please watch me solve the problem, and you'll learn how to do this, and then that way, if you get stuck, you come here and you look at the answer key, you get the answer, and then you learn something, and you move on, and you should be able to do the next slide without having to come back to the answer key for help. So learn from this, don't just copy. If I catch people copying, I won't provide these answer keys in the future. So this is something you can lose, but it's something I hope will help you out quite a bit and teach you how to code. So this is while loops and farmer, the first three problems don't actually use while loops. They should be solvable by you without help. Because these are things that we've done before with repeat blocks and moving forward and things of that nature. So in this case, the farmer needs help to flatten the field on the farm, so it's ready for planting, and all of the problems are going to be involving that. There's going to be piles of dirt or holes with out dirt, and you need to either remove dirt from the pile, or fill the hole with by adding dirt to it by filling it. So in this case, we have a pile. This is what a pile looks like here in the option. There's only one shovel full of dirt that we need to take off, but we need to move our character there. And we need to then remove the dirt. So in this case, we need to move forward one, two, three, four, count it out four times. So but we only have one move forward block. So we really can't bring more move forward blocks out. So we're going to say repeat. We're going to have her repeat, move forward four times. And that will get her to the dirt pile. And then when we get to the dirt pile, we need to remove one shovel full of dirt. Now, we could put this remove inside the repeat block and but if it's inside this repeat block, that means she's going to move forward and then try to remove dirt and move forward and try to remove dirt and move forward and try to remove dirt from places that don't need dirt removed. These first three squares don't need dirt removed. So you're really just slowing her down and you're causing problems. So if we take this remove and put it at the end of the repeat block and we hit run, she'll move forward and she'll take the dirt away. And that is the answer. The next problem is a little bit similar, but it's in reverse. So in this case, we need to move, move her to the hole, and we need to fill the hole with dirt. So this is a hole that we need to fill the dirt. There are 6 shovel folds that this hole is going to need to be filled. So we're going to have to fill this whole 6 times. We also need to move our character to the hole. So we're going to move her forward, but only one time, because we don't need to repeat that. But we do need to repeat the fill procedure in order to fill the hole. So we're going to repeat, fill one. Now if I run this, it's only going to fill the whole 5 shovel fulls of dirt so we need to change this to 6. And if we run that, she'll move forward. She'll fill the hole 6 times and the problem will be solved. In this problem, very similar to the previous one, but we have a pile of dirt that we need to remove, not fill. It's not a hole we need to fill. It's a pile we need to remove. So in this case, we'll move forward one. And repeat that remove procedure, and we have ten shovel folds that we need to do. So we'll put ten in the block. And we should have the answer. So these three were pretty easy. We haven't used the while block yet. They haven't given it to us, but the next screen is a video on how to use while loops. You should watch it. Basically, it's about how you take a while command and while something is true, it will do something for you. In this case, we could say, while there's a pile, remove the dirt, while there's a hole fill the dirt. And in this case, we have a question, what do we think is going to happen? If we look, it's going to move her forward to the pile, and while there's a pile there, start removing a shovel full of dirt. So in this case, it looks like she'll move forward and she'll remove dirt 8 times until there's no more dirt left, and then when there's no more dirt left, and there's no pile, this will stop running. So if we look, we'll say nothing will happen. The farmer will never stop removing dirt, the farmer will move dirt as long as there's a pile then stop or I don't know, and we just solved it. It's number 6, excuse me. And on the next slide, we're going to actually use the while loop. So here we have a question mark. We don't know how much dirt is there. If we run the program, it might change to an actual number. But if we run the program again, it might be a different number entirely. So you can't run the program, if you could try to run the program and see how many shovel falls of dirt are there to remove, but if you run it again, it might change. So in this case, we need to do the wild block because we don't know. So we'll move forward to get to the pile. And then while there is a pile, we'll remove dirt, okay? And this will keep running until there's no more dirt left. So in this case, it looks like they were 12 shovel fulls of dirt to remove. And it's going to remove all of them. With four blocks of code or three lines of code, excuse me. Okay, on the next one, this one got a few 6 DL people the other day. In this case, we have blocks, question marks, then a number, question mark than a number, and then a question mark. So we have three piles of we don't know the answer. So the long way to do this would be to move forward and then while there's a pile, remove a dirt, and then you can do move forward again, and then repeat 5 removes to remove the 5 there, right? But that would be very long, and it's completely unnecessary. Because we can say for each step 5 times, so we'll move forward 5 times. And then when we get there, well, there's a power move one. So let's see how we nested this conditions. We put a repeat block on the outside. 5 times for 5 different piles, move forward, and while there's a pile there remove it. So it will handle the question marks, and it will handle the 5. Number blocks. So if we run this, we should see her move along and remove all the dirt. Okay, next one. This one's a bit trickier. This one is a little bit tougher, but if we break it down, it's not too bad. So if we look, we need to have her move forward. So we'll start there. We'll just start breaking it down a little bit by little. And we'll be on a whole that we need to fill. So we'll say, wow, there's a hole. Fill it. And then she'll be done there. And then we need to turn her and move her to get her to the next block. So if we say turn to the right, and we put this after the wild block, we don't put it inside, put it outside. And then move forward, she'll then be down here on this block. And if you want, you can always run the coat, make a couple changes, run it, make sure you got it right. And then go back and do more. So a lot of times when you're building something, try building a little piece of it, test it, try building a bigger piece, test it, it's not always a good idea to try and solve the entire problem right off the bat. Try to do a chunk at a time. So that moved her. So anyways, you can see the result of this is that she moved down here, but she didn't remove this pile. So we still have more to do. So we need to, well, there's a hole. Fill it with dirt. And then to get her to be able to face the next hole, we need to turn her back to the left, I believe. So if we run this, shall move over, take the dirt away, turn and move, take the dirt away, and turn and get ready for the next step. Now here is where it gets interesting. If you look, it looks like the next pattern in the next two slots are the exact same thing we have here. We could repeat this exact code right here. Two times and it would get hurt to this block of 5 and remove all the dirt. And in fact, we could do it three times and get all the dirt. So instead of having to copy this, all of this code again and have 30 different blocks of code, we can just add one more repeat block and put this in here and she'll get it done. So if we look, it says 5 times, but we got her to hear with just this code, so we really only need three times. So if we say repeat three times and run this, that is the answer. And there we go. Okay, this one isn't too bad. You can see that we have a whole four holes we need to fill. There's only one fill at each spot, but there's a new condition here, which is while there's a path ahead, which means if she's facing a direction and she can move in that direction, then go ahead and move her forward. And you can see they only give us one move forward block. We can technically drag it out, but it won't be the answer if we drag out more than one move forward. So in this case, if the path ahead is clear, move forward. Let's see what that does. That should move her all the way to number one here. And you can see the little radar. That's the wild path ahead testing. So in this case, you'll move all the way to the end. And she'll stop because now there's no path ahead. So when she gets here, she needs to remove that dirt. And then that will clear this pile. And then she needs to turn to the left to face up. And if we do that, that gets her ready to move forward and get to this pile, and you can see that she will stop at this spot. And if she turns and goes here, she'll stop and remove this block. So this is actually all the code that we need. We just need to have her repeat it a number of times. And if we look, if I reset this code, she'll need to walk forward, remove that, walk forward, remove that. That's two. Walk forward, remove that. That's three. And then walk forward and remove that, that's four. So this is a repeat four times block. And if we run that, that should take care of this lesson. So I think this is the last lesson in the next actually, I think we have a multiple choice question next and then after that, our challenges. See that I put in remove instead of fill, so it actually added a number instead of instead of taking the pilot away. So this is actually going to fail. So in this case, we need to change the remove to fill and then turn. So I made a mistake there. So that's why it's a good idea to test your code in small chunks and build a bigger program by using tested code rather than try to do it all at once because you can make mistakes like that. And when you have tons of code and you make one small mistake somewhere and you have to go through a giant block to try and figure out where you made that one little mistake, it's a lot harder than when you have a couple of lines of code and it's very easy to test and see where you messed up. So in this case, here's the solution. I'm sorry, I was wrong. This is the challenge section. So I'm not going to give the answers to the challenge puzzles. If you want to do the challenge puzzles, you can stop here and do the challenge puzzles. And for every challenge puzzle, you get right, you'll get extra credit, or if you haven't completed lesson 8 debugging in collector, then you can also go back there and fix up your grade by going back to lesson 8 and debugging. So I'm not going to give you the answers to these challenge puzzles. I want you guys to try and show me what you've learned. Okay? Enjoy. If you have any questions, please raise your hand. Let me know. Thank you.