video

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 programme 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 programme that uses nested selection.

Shall we make a start? We will be using this keyword 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 programme 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're 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 programme closing.

So you can see each time we walk through the programme, 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 3 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 think 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 > 10.

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

Then the selection statement on line six, which if books > 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 and 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 programme that uses nested selection.

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

Izzy says, "So I start with a programme 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 programme 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 username == "Izzy," elif username == "Lucas" and so on.

For as many usernames as we want.

Time to check your understanding.

Modifying a programme 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 programme means that you edit a programme 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 programme, 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 programme from experiencing errors.

Time to check your understanding.

What should be added on line two of this programme 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 == "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 programme, oak.

link/guess-the-animal.

Run the programme 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 programme.

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 are asked to run the programme again.

And enter a 2 for the first question.

What is the output? Again, the output should have been, "It must be a Whale!" For c, you'll 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 programme 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 programme 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 programme 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 programme, 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 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.

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 are saying answer = input().

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

On line 16, we have if answer == "n" and then we have an indented print statement which says, "It must be an 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 summarise 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!.