Lesson video

In progress...

Loading...

Hello, my name is Mrs. Holborow and welcome to Computing.

I'm so pleased you've decided to join me for the lesson today.

Today we are going to be looking at how we can nest selection statements to provide more possible paths for a program to follow.

Welcome to today's lesson from the unit "Programming Selection." This lesson is called "Nested Selection." And by the end of today's lesson you'll be able to trace and modify code in a program that uses nested selection.

Shall we make a start? We will be using this key word in today's lesson, nested selection, a block of code where a selection block is placed within another selection block.

Today's lesson is broken down into two parts.

We'll start by describing nested selection and then we'll move on to modify a program that uses nested selection.

Let's make a start by describing nested selection.

When a selection block is placed within another selection block, it is called nested selection.

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

What do you think will happen when this code is executed? I'd like you to think about what would happen if Jun was entered for the username.

What would happen if Izzy was entered for the username? And what exactly needs to be inputted to gain access to the system? Maybe pause the video whilst you have a think about those questions.

Izzy's got a great response here.

To gain access to the system, Izzy needs to be entered for the username and Fish4321 needs to be entered for the password.

Let's walk through this code together.

So the first line of code that's executed is the print statement on line one, which says print username.

We then take the user's input and store it as username.

So in this case, we are going to use the username Jun.

We then move to the selection statement on line three if username is equal to Izzy.

Well, Jun is not equal to Izzy, so this is false.

So we are going to skip all the way down to line nine and we're going to print program closing.

Let's walk through again.

Again, we start on line one and we print username to the user.

We take in the input and we store it as username.

This time we are going to input Izzy.

We move to the selection statement on line three and check if username is equal to Izzy.

This time it's true.

So we are going to move to line four and we're going to print password out to the user.

We are then going to store what the user enters as password, and then we're gonna move on to the selection statement on line six.

So here the user has added the password, Cat1234.

So that's not equal to Fish4321.

So that selection statement is false.

So we're gonna skip to line nine and we're going to print program closing.

Let's walk through a final time.

We are going to print username from line one.

We are then going to store the user input as the variable username.

So we're going to use Izzy again this time.

So the selection statement on line three is true.

We are going to print password and we're gonna store the user's input as the variable password.

This time, the user has entered the password Fish4321.

So the selection statement on line six returns true.

So we are going to execute line seven this time and we're going to print Access Granted.

We then execute line nine and say program closing.

So you can see each time we walk through the program it behaves slightly differently depending on what the user had added.

Time to check your understanding.

When a selection block is placed within another selection block, it is called a? Pause the video whilst you have a think.

That's right.

When a selection block is placed within another selection block, it is called a nested selection.

Here's some examples of nested if statement structures.

So you can see on the left hand side I have an if with a condition and a block of code, and then I have a nested if with another condition and a block of code.

In the middle I have an IF statement with a nested IF ELSE inside.

And on the right hand side, I have an IF statement with a nested if, elif and else.

So lots of different types of selection statements can be nested inside each other.

Time to check your understanding.

Assuming that 12 is entered for the first question and three is entered for the second, what will be the output? Is it A, I hope you like reading.

B, that's impressive.

Or C, you should read more.

Pause the video whilst you look carefully at the code and about the answer.

Did you select C? Great work, the code will output, you should read more.

That's because the first question was what is your age? If 12 is entered, then the selection statement on line three is true because age is greater than 10.

If we then enter three for the next question, which is the total books read this year.

Then the selection statement on line six, which if books greater than 10 is not true.

So the code is going to skip to line eight and execute the else statement and the indented print statement on line nine, which says you should read more.

Okay, we are now moving on to our first task of today's lesson and you're doing a fantastic job so far, so well done.

I'd like you to describe what is meant by nested selection.

Pause the video whilst you have a go at the activity.

How did you get on? Let's have a look at a sample answer together.

Nested selection is when a selection block is placed within another selection block.

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

For example, you could have an if inside an if statement, or an if elif and else inside an if statement.

Remember, if you need to go back and revisit your answer or add any detail, you can pause the video and do that now.

Okay, we are moving on to the second part of today's lesson where we're going to modify a program that uses nested selection.

Modifying a program means that you edit the program to change its functionality.

Izzy says, "So I start with a program that isn't mine/" Andeep says, "Yes, and then you gradually make it more your own." That's a really great idea Andeep.

How could this program be modified for more than one username? Maybe pause the video whilst you have a think.

The first selection statement could have multiple paths.

For example, we could have if the username is equal to Izzy, elif, username is equal to Lucas, and so on, for as many usernames as we want.

Time to check your understanding.

Modifying a program means starting from scratch.

Is this true or false? Pause the video whilst you have a think.

That's right, it's false.

Modifying a program means that you edit a program that has already been started.

When checking user input in selection statements, it's sometimes difficult to predict what a user will enter and how they will enter it.

For example, in this program, the user may not type a capital I at the start of the name Izzy.

They may type Izzy in all lowercase or even Izzy in all uppercase to resolve this problem.

Python has two methods that can be used upper and lower.

The upper method will return a string of all uppercase letters.

The lower method will return a string of all lowercase letters, so whatever the user enters, the method will convert the characters to either upper or lowercase, depending on which one you use.

So this will convert whatever the user inputs into the format you want to check in your selection statement, which is really handy and can stop your program from experiencing errors.

Time to check your understanding.

What should be added on line two of this program to ensure the user input is correctly checked in the selection statement.

Is it A,.

upper B,.

lower.

or C,.

case.

Pause the video whilst you have a think.

Did you select A,.

.

upper? Well done, in the selection statement on line three, we have if ticket is equal to Y in uppercase, so we want to force the user input to be uppercase to check it correctly.

Of course, this may not solve all problems as the user may type the whole word yes rather than just the initial.

Okay, we are moving on to our second task of today's lesson, task B.

Open the starter program.

Oak.

link/guess-the-animal.

Run the program and answer these questions.

Pause the video whilst you have a go.

Here is some more questions that I'd like you to answer whilst running the program.

Pause the video whilst you complete the questions.

Let's have a look at the answers together.

For part A, you were asked to enter a Y for the first question.

And then you asked, what is the output? The output should have been, "It must be a whale." For B, you were asked to run the program again and enter a two for the first question.

What is the output? Again, the output should have been, "It must be a whale." For C, you were asked the question, what needs to be input for the output to be, "It must be a whale." The input can be anything apart from an N.

D, run the program again, enter an N for the first question.

What is the output? The output should be, "Does the animal have wings? Y/N" E, which line of code is executed when the condition on line six is true? Line seven is executed.

F, which line of code is executed when the condition on line six is false? Lines 13 and 14 are executed.

For G, you were asked to run the program again and enter an N for the first question and an N for the second question, what is the output? The output would be, "It must be a lion." H, what needs to be input for the output to be "It must be a lion." They must enter an N first and then they can enter anything apart from a Y second.

I, run the program again, enter an N for the first question and a Y for the second question.

What is the output? The output is, "It must be an ostrich." J, which line of code is executed when the condition on line nine is true? 10 is executed.

K, which line of code is executed when the condition on line nine is false? Lines 11 and 12 are executed.

F, does the user have to enter a lowercase N or Y for the code to execute correctly? Explain your answer.

No, they can enter a capital one and it will still work.

The code lower converts the value entered into lowercase.

For part three, I'd like you to make some modifications to the program.

A, at line 14, enter a new line of code that outputs, "Is the animal a mammal? Y/N" B, at line 15, enter a new line of code that holds the user input in the answer.

C, at line 16, enter a new line of code that will check if the answer to is it a mammal is equal to N.

D, the program 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.

E, enter the necessary lines of code to make this happen.

F, test your code to make sure it works correctly.

Pause the video whilst you have a go at the activity.

How did you get on? Let's have a look at the code together.

If you want to open the full working solution, you can go to oak.

link/guess-the-animal-solution.

We're only gonna have a look at lines 14 onwards here because the rest was already there for you.

So on line 14 we have an indented print statement which says, "Is the animal a mammal? Y/N." On line 15, still indented because it's nested.

We're saying answer is equal to input.

lower, so we're forcing the user's input into lowercase.

On line 16, we have if answer is equal equal to N and then we have an indented print statement which says "It must be a fish." On line 18, we have else, and on line 19 we have print, "It must be a whale." Be really careful with your indentation here.

When we are nesting, we have to make sure our if and selection statements are indented to the correct level.

Okay, we've come to the end of today's lesson and you've done a fantastic job, so well done.

Let's summarize what we've learned in today's lesson.

Selection statements with conditions can be nested inside another selection statement.

Nested selection statements are used to implement complex branching logic.

If a condition of a main section block evaluates to true, then it leads to sub conditions which are included inside the main condition.

I hope you've enjoyed today's lesson and I hope you'll join me again soon.

Bye.