video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher for the programming Part Two unit.

For this lesson, you're going to need your replica account, which you should have already set up with your parent or carer's permission.

You're also going to need a pen and paper to make any notes or answer any of the questions that I give you in this lesson.

And it's a really good idea to try and make sure that you remove as many distractions as you possibly can, so that you can really focus in this lesson.

Once you're ready, we can begin.

In this lesson, you will define nested selection, walk through code that uses nested selection, and modify a programme that uses nested selection.

Let's start off with a quick recap of what we learned in last lesson.

What will these logical expressions evaluate to? You can copy and complete this table or just write True, False, False down on a piece of paper that's fine.

So pause the video while you have a go.

Let's take a look at the answers then.

So the first one was False.

The second one True.

The third one was True.

And the final one was False.

We're going to make another prediction now.

So, explain exactly what you think will happen when this is executed.

And I want you to think about what would happen if Ben was entered for the username? And what would happen if Eirini was entered for the username? And what exactly needs to be inputted to gain access to the system? So pause the video while you have a think about that.

Let's take a look at them.

So when a selection block is placed within another selection block, we call this nested selection and this is what you've just seen on that programme.

Any type of selection block that can be nested inside a selection block.

And these are just some example structures for nested selection.

So you can have an if with just one if in it, you have an if and then an if else, you have an if and then with another if, elif, else in it.

There's lots and lots of different combinations, and they're not the only ones.

So let's take a look a deeper look into that programme we just had in our prediction.

So first of all, you've got print username, so what would have displayed username on the screen.

And then at that point, if the user entered Ben, then the state of the variable username would be Ben.

And then you've got a condition.

So if username is equal to Eirini, and this would be false at that point, because it was false, it would not run any of that code that within that block, and it would go straight to line 10 to programme closing.

If we run this programme again though, and we have input username same thing, but this time we put Eirini, this time, this part of the condition is going to be true.

And then we have password.

And then if they input the password Bob at this point, then if we go to the condition, that condition is false.

Because it's false, it doesn't run the next block of code, it goes straight to the final line, which is to programme close.

Then let's look at the other the other scenario.

So, username this time put Eirini.

Yes, it's true.

So it's going to print password and it's going to ask for input.

But this time when "Fish4321" is entered, because that condition is true, then this block of code is run as well.

And then also, once that state.

those block of statements have finished, it goes back to the main part of the programme and he does that final line.

So that's how nested selection works.

What I'd like you to do, though, is actually investigate a programme that uses nested selection.

And you're going to look at I guess that animal game.

So explore a modifier guess the animal game to deepen your understanding of nested selection.

Pause the video while you have a go that.

Brilliant, now I'm going to go through those solutions now with you so that you can see if you got stuff or just to see if you got it right.

So let's go and have a look in replic.

So the first question in the investigate step is to enter a Y and see what the first output is.

So I'm going to do a lowercase y as well.

So y, and the first output is "It must be a whale," okay? Run the programme again.

Enter a two for the first question, what is the output? It must be a whale, hmm that's odd Okay, so what needs to be the input for the output to be "It must be a whale?" So let's have a look.

So if the answer is n, then it's going to do "Does the animal have wings?" Then it's going to say, if it's a Y, it's going to say "ostrich." And if they say, no, it's going to be "lion." But if they put anything else in this scenario, then it's going to print "It must be a Whale." So I could put anything I liked here.

And it's just going to go to that else statement, "It must be a Whale," all right? And then we've got Step four, we're in the problem again, enter an n for the first question, what is the output? Let's take a look, so n.

So the output is "Does the animal have wings? Y or N." It says which line of code is executed when the condition on line six is true.

So which line of code is executed when it's true? So this line of code is executed when this is true.

But which line of code is executed when the condition on line six is false? So if it's false here is just going to go to this one, isn't it? Because it's not true.

So it can't go any further within here.

So it's going to go to the next block that's there, which is the else block.

Okay, so then we've got Step seven.

Run the programme again, enter an n for the first question and an n for the second question, what is the output? So n, n, "It must be a lion" is the output and what needs to be the input for the output to be it must be a lion.

So they have to put an end first and they could put an n next, but they could also put let's just say, if they put an N, and then they put gobbled ago, "It must be a lion." So it's always going to that else within there.

Okay, so then it says run the programme again, enter an n for the first question and a y for the second question.

And so let's see what happens now as we've got n, y, it says "It must be an Ostrich." And then you've got which line of code is executed when the condition on line nine is true.

So, if this is true, then this line of code is going to be executed because it was true that time and it's now saying "It must be an ostrich" so that line of code is running.

Which line of code is executed when the condition on line nine is false? Well, because we've got a new and nested if else within here, because this is false, it's going to go to this else here.

And then it's going to print "It must be a lion." Then he says, Does the user have to enter a lowercase n or y for the code to execute correctly? Explain your answer.

Now, they don't do that because you have got the lower function every call to it's always going to return a lower case value.

Then we've got the Modify step.

So it says at line 14, enter a new line of code that output "Is the animal a mammal?" So at line 14, I'm going to put, I'm going to leave that there just for now it doesn't say to delete it.

So, enter a new line of code that outputs "Is the animal a mammal?" So print, "Is the animal a mammal?" Okay, yes or no? All right, at line 15, enter a new line of code that holds the user input in answer, some of your answer equals input, and I'm going to make sure I keep that low a bit on there as well.

Test your code, check the input output in the hint to see if it is working correctly.

So it should say "Pick either ostrich, lion or whale." "I will attempt to guess your choice." "Does the animal live in the water?" "Is the animal a mammal?" "Yes, it must be a whale." Now, I haven't It doesn't say to put an if statement in there at this point in time.

So I don't think I have to do that yet.

Let's just say so, I will just check it.

So line 15 enter a new line of code that holds the user input in answer and it says test your code, check the input output in the hint to see if it is working correctly.

So I am going to run it.

And it tells me to put a y and then a y.

And then it does say "It must be a whale" at that point.

Then it says, at line 16, enter a new line of code that will check if the answer to "Is it a mammal?" Is equal to n.

So at line 16, okay.

So check if animal is equal to n, okay? Then what does it want me to do? The programme should output, "It must be a fish" if the condition on line 16 is true.

And "It must be a whale" if the condition is false.

Enter the necessary lines of code to make this happen, okay, so got a little bit of thinking to do here.

So if animal is equal to n, then I want it to say it must.

Let me just look at it again.

So if the condition on line 16 is true, it needs to say "It must be a fish." Okay, otherwise, oups, I'm got the right one there.

So, go into the right line.

Oh, there we go.

Else, "It must be a whale." Don't worry, it's not lining up.

It's a little bit annoying that Okay, I've got it to line up now, though I think.

It doesn't line up.

We'll have to mess with that a bit later, okay.

And looks like it's working anyway.

So test your code, check the input output in that when the hint box to see if it is working correctly.

So if I type y, to get me to the else, and then is the animal a mammal, if I put an n, it should say it must be a fish.

And it doesn't 'cause animal is not predefined.

See, I made an error there.

Did you spot that error before me? So it's not meant to say animal, is it's meant to say answer.

This is why it's really important to check your code.

So answer, let's run it again and see if it works.

So y, "is the animal a mammal?" "It must be a fish." So that one's working.

And then I'm just going to do y, y, "It must be a whale." So now we work in, took a little bit of thinking about it.

So then it says tissue code check the input output is in the work we have done now.

Test your code again, check the input output in the hint to see if it is working correctly.

So pick either ostrich, white alert, I've already done it.

I've already edited.

And then it says edit the instructions at the beginning of the code to reflect the addition of fish to the animal choices.

So we can put fish there, so now just run it.

So now I can play it properly.

Say, "Pick either ostrich, fish, lion or whale" and I'm going to pick the lion.

"Does the animal live in water?" No.

"Does the animal have wings?" No, "It must be a lion." So now it's working, isn't it? Okay, so you can follow along with me and fix that yourself.

And you can see it does take a little bit of thinking and stopping and pausing and trial and error and problem solving.

And just going to get everything right perfect the first time, this is not how programming works.

So hopefully made struggling a little bit there helped helped you to realise and see that actually, programmers do make little mistakes and do have to stop and think here and there in order to get their programmes to work properly.

Now you've had a go at investigating the animal game, you're going to actually use very, very similar code to create a guess the vegetable game.

So use what you have learned in the previous task to create your guess the vegetable game.

Now, you have not got as much help as you have in the past to do this activity cause I want you to try it and really see if you can problem solve and try and figure it out yourself.

And that's why it's really important to look at the code that you've just done.

And use that as a grounding as a structure for the problem that you're just about to solve.

Now, you don't get as much help but you do still get those sample images output boxes so that you can go through and you can test your code as you go along.

And it'll also give you a bit of a clue about the type of code that you might want to actually use.

But give it a go, try and do it independently if you can.

But don't worry, I am going to go through it all with you in a few moments, if you need some extra support, but do, do try your best to see if you can figure it out by yourself.

So pause the video now or you have a go later.

Excellent, then.

So let's see how you could have completed the activity.

Let's get on and have a look in replica.

So I've got my make the vegetable worksheet in front of me.

So I'm going to try and programme this as well.

I've also made sure that I've kept on another tab on my screen, I've got my animal game code too.

So I can refer back to that if I need to.

Cause the structure is very, very similar.

But obviously this time, instead of animals, we're looking at vegetables.

So I'm just going to go back to my blank screen, think about how I'm going to get started.

Now for this one, we've got peas, broccoli, carrot and sweetcorn as the four options that it could be, and then you've been given a decision tree to help you programme along to so you can start looking at those branches.

And on the decision tree, the first example is the vegetable green yes or no.

So we're now going to have to have that at the beginning.

But before they even have happens, I need to have some instructions.

And if I look at my section worksheet that says sample input output for testing, the sample print that we've got there is "Pick either carrot, broccoli, peas or sweet corn." So I'm going to start my programme with that.

So "Pick either carrot, broccoli, peas, or sweet corn." And then also underneath it says, "I will attempt to guess your choice" like that.

And then it starts with the question.

So is the vegetable green? Which is the same question that is at the start of that decision tree that you've got.

So print "Is the vegetable green," and then you've got a y and n.

So all of that I can pretty much just copy off the input output table that's there in the worksheet, because it's all just print statements.

But now I've got to start thinking about the actual structure those selection statements and the nested selection statements too.

Now, I can look at my code that I've got on my animal one.

So what did I put next after the instruction? So you've got the instructions there, then you've got "Does the animal live in the water?" And then you've got answer equals input.

lower.

So I'm just going to have exactly the same thing in my programme like that.

It's the same structure, may as well use the same variable, there's no point having a different variable because it's a similar thing.

So I've got that bit right.

And then the next bit says if answer is equal to no, so I'm going to also copy that bit, okay? But now what I've got to do is I've got to look at my decision tree, because this isn't the animal game, this is the vegetable game.

So I've got to make sure that I'm looking at my decision tree there to help me.

So, when he's got is the vegetable green, if they put no then the next question is easy to arrange.

So and I can always check that as well with my input output.

Because actually, the next question on there is the vegetable orange.

So I know I'm on the right lines.

So I'm going to put "Is the vegetable orange" and then y/n like that.

I'm just going to check my structure again with the animal one.

So is it got a similar thing? Yeah, so it's saying does it have wings? Yes or no? And then we've got this answer.

Block of code again, underneath, like so.

And if they type in a with a y is going to say, "It must be a carrot." And if they say no, it's going to say "It must be sweetcorn." So again, I can look at what happened here.

So here I had, if answer is y, "It must be an ostrich." But if it's.

But otherwise, else, "It must be a lion." So I can have pretty much the same code again.

Can I just use that same code again? That's why it's there.

So if answer is yes, so if it's got to orange and then it's yes, I'm just looking at my decision tree just to double check.

So if they put no for vegetable, it says "Is it orange?" And if they put yes for is it orange? Then they put "It must be a carrot." So if they put in y, It says "It must be a carrot," like that.

And then I've just got my else space which is the other option on that decision tree.

So if they put a no, it should say, "It most be sweetcorn." So I've done now that whole first section.

So if they put a no an n, then it's going to say, "Is it the vegetable orange?" And if I put an n, it should do "It must be sweetcorn." And if I put a y it should be, "It must be a carrot." So I'm just going to run that programme.

Hopefully, I've got no errors this time.

So I will attempt to guess your choice "Is the vegetable green?" I have to put in this time because there's no other option.

"Is the vegetable orange?" So I put a yes.

"It must be a carrot." So that it's working, and then if I just put an n, it should give me the other option.

Yeah, "It must be sweetcorn." So that part of the programme is now working.

Now I need to look at the other branch that is on my decision tree.

So this one is for if they put a y, so I just need the else.

And if I go back to my animal code, I can see here that the else for this one, needs to be in line with this if here, because it's part of this if else block.

Okay, so I've got to make sure that I put my else there.

And I know that I've got to put some code underneath.

And also on my worksheet, I've got that table, that input output table.

And it's also got testing for the other branch.

So I can see what had to be happening here.

So it says, the next question is "Does it look like a tree?" But I've got to just double check this.

'Cause is that what we've got here? So we've got here else and then we've got a print with another question.

So yeah, it's definitely right.

So "Does it look like a tree? Y/N, okay.

And then again, if I look over here, I've got this answer block again, I can just copy that over.

And then just like we've got this structure here with our if and else, let's just double check.

Yep, we've got an if and else here.

So I've got if and what does it need to be? Let's have a look.

What does it say on my input output? So just a vegetable.

So if I type no, it should say it must be all peas.

Oh sorry, it says it should be "It must be peas." And then the other option should be "It must be broccoli." So if answer is equal to y, and I can look on my decision tree just to see if this is right.

So if they've typed a y so for yes, and then they type another y then it should say, "It must be broccoli." And then else print "It must be peas." All right here hey, so now I just run it and test it for the other option.

So now I'm going to put a y, and see if that branch is working.

Yep, "Does it look like a tree?" y, "It must be broccoli," and then I'll just see it for the over option.

So y and then n, "It must be peas." So my programme is now working.

Let's end the lesson then with some questions about nested selection.

So take a look at this code.

Assuming that 12 is entered for question one and three is entered for question two, what will be the output? Pause the video while you have a think about that.

And the answer is, "You should read more." Here's the next one then, assuming that 10 is entered for question one, and 10 is entered for question two, what will be the output? Pause the video while you think about that? And the answer is "The programme will never reach question 2." That was a bit of a trick question there wasn't it? So you have learned about nested selection in this lesson.

You've had to go modifying an animal game and you've created your own vegetable game.

So that is quite a lot to be getting on within one lesson.

Plus, you've had a lot of a go at creating a programme all by yourself with minimal support too.

So don't worry if you had to code along with me, that's fine if you still in that situation where you need to have that extra support, then that's fine.

I'm here, I make these videos to help you.

But if you are starting to be a bit more independent with your code too, then well done, that's really, really good start.

And I'll see you again soon for another unit which will be part three.

So programming Part Three is the next one that you'd want to do after this unit.