Loading...
Hi, I'm Rebecca, your computing teacher for the programming part two unit.
Now for this lesson, you're going to need your rapper to camp, which you should have already set up with your parents or carers permission.
You are also going to need a pen and paper so that you can answer any of the questions that I give you in this lesson.
It's also a really good idea to try and remove as many distractions as you possibly can so that you can really focus in this lesson.
Once you've got all of that ready, we can begin.
In this lesson, you will identify flowchart symbols and describe how to use them.
And we're looking at the decision symbol in this lesson, we'll define a condition as an expression that can be evaluated to either True or False.
And we'll identify that selection uses, conditions to control the flow of execution.
And we'll walk through code that includes selection.
So looking at if, elif and else.
Let's start by making a prediction then.
What I'd like you to do is take a look at this flow chart.
And it's got a new symbol this time.
So you might not have seen it before.
You've possibly seen it in a design and technology lesson, or maybe a science lesson before, but you haven't seen it in a programming lesson before.
So you might be able to predict what this actually does.
So if you input the number 10, what will be the output? So pause the video while you have to think about that.
Let's see then.
So 10 is more than five so this will be True.
So the output will be 15 because it's going to follow the branch that goes down rather than to the right.
The decision symbol is used in a flow chart control the flow of execution.
Inside the diamond is a condition.
If the condition is True, then during the programme execution will follow one path.
If it is False, it will follow the other path.
So what is a condition? You already know that arithmetic expressions evaluate to a number.
This is what we looked at in the last lesson.
So if we have number equals 30 + 3, and it will evaluate to 33.
Logical expressions evaluate to True or False.
So if you look at this one, that's highlighted, score is more than 30 could either be True or False, but because score there is 20, it is evaluating to False.
So will the following logical expressions evaluate to True or False.
I'm going to show you these on the screen as we go along.
So what do you think? Have a look, three is greater than four.
Is that True or False? It's False, isn't it? This is a little bit tricky you might want to pause your video while you figure it out.
So 3 + 10 is less than 3 x 10.
So have a think about that.
Is it True or False? It is True, isn't it? 'Cause 13 is less than 30.
Even harder now and we're trying to go back to that big maths that we were talking about as well in last lesson.
So see if you can remember some of that big maths.
So what we're saying here is if you look at the symbols below, you've got two equal signs means equal two.
So we saying the bit on the left is equal to the bit on the right there.
So pause the video while you have a think about that.
And the answer is True.
So can you use a logical expression in a condition to control the flow of execution in your programmes.
Just like we saw with that flow chart.
Here a condition is being used in a selection statement.
And this is also known as an if statement.
Selection statements control the flow of execution because a block of code will only run if the condition is True.
I'm going to walk through this code now.
So score is assigned 20.
So the stay of that variable is currently 20 score is not greater than 30, so the condition is False.
The flow of control moves to line four and outputs to the end.
So let's just recap that and have a different value in score that say this time score is assigned to 35.
So the state of that variable is 35 and score is over 30.
So this condition is now True.
The flow of execution moves to line three and output you won.
The flow of execution then carries on to line four and output the end.
We can also add another branch.
So you can provide another branch to your selection statement by using if else when you use if else you are saying, if this condition is True then do this else do this.
And here is an example of using else.
So we've got their print, what's your name, user and it's asking for some input, if user is equal to Elizabeth print good morning else print, Hello user.
So the conditional check, if the value of user is equal to the string, Elizabeth, the expression user is equal to Elizabeth will evaluate to either True or False.
This is the if-block or the code will be executed if the condition is True.
And this is the else-block or the code that will be executed if the condition is False.
Only one of these blocks will be executed depending on the value of the condition.
And can be more branches.
So you can provide further branches by using elif when you use if and if-else you are saying, if this condition is True, then do this else if this condition is True then do this else do this.
And here's an example of using elif.
So if the expression user is equal to Elizabeth is evaluated to False.
The elif condition will check if the value of user is equal to the string, Alan Sugar.
If this is True, the elif block will be executed.
The else-block will be executed if both conditions are False.
Only one of these blocks will be executed depending on the value of the conditions.
You're going to have a go at looking at some of these now delving a little bit deeper.
So go to the what will be the output section of your worksheet and complete the tasks pause the video now and half ago.
Excellent, so let's go back and look at those solutions to that worksheet.
So it was to copy and complete the table to state what will be the output based on the input.
And those were the correct answers.
So you can pause the video now, while you correct your work or mark it.
And then for programme two those were the answers.
So pause the video again, while you mark your work.
And this was task two programme one we're looking at if else this time.
So pause the video while you mark it.
This one was programme two And then this was the final task if, elif, else, and this was programme one.
So pause the video while you mark that.
And then programmed two pause the video.
Excellent, now we're going to use what you've learned back to look at a chatterbox programme.
So go to the chatterbox section of the worksheet and complete the tasks on there.
And if you do struggle with that I am going to do a demonstration and that's coming up so you can get a little bit of support there if you need it, but do try and work on it independently before you come and look at this video, pause the video now while you have a go.
Excellent then, so I'm going to go through the solutions now, if you did struggle with that a little bit then you can code along with me if you need to, or you can just use this to take make sure that you got things right as you went along.
So let's go and take a look at this in replay for the chatterbox activity you were first asked to make a prediction and run the programme.
So when you were making your prediction, you should have had a really good read of the programme to see if you could try and understand it and try and figure out what might happen when is executed, or you might have not made a perfect prediction.
You might've got a little bit of the way there, but it's say, okay, we're all learning.
So don't worry if you didn't quite get it exactly right when you run it.
And what I'm going to do is I'm going to go through the investigation with you.
So the first part of the investigation was it says, execute the code and type Anakin upper case.
And see what's happened when it's immediately underneath.
So I'ma going to type in upper case, what is your name? And then what it's done straight away underneath.
It says Anakin in lower case The next step was to type in lower case.
and see what happens this time.
So I'm going to put on Anakin in lower case.
I spell it right first.
So I put it in lower case and it still says, and it came back to me but it's repeated back in lowercase.
So both times it repeated it back in a lower case.
Then he says, go to line two and delete dot lower.
I'm not going to delete it.
I'm not going to put the hashtag in front of it so that it gets ignored when it's executed.
And then it says type Anakin in upper case, this time I'm going to do to see what happens.
And this time directly underneath it is just saying, Anakin in capital letters.
Then it says do it again but do it lower case.
So I'm going to do it low case and its doing in lower case.
So that lower is obviously that dot lower there is obviously quite important.
It says at the lower, the dot lower code back in at the end of line two.
So I'm just going to get rid of that.
What function do you think dot lower performs. And now I know what it does, what it does is it returns a string all in lower case.
So if I enter it all in upper case it's going to we're going to pass that into the function and it's going to be returned as a new value but all that same word that we typed in but all in lower case this time.
Then he says line nine has stopped operating the end of input.
So let's just go to nine line.
Yep so we've got dot up her there.
What function do you think this performs? Now it says to actually do some more investigative tasks but I already know that dot upper is just the opposite of dot lower, which returns or lowercase dot upper returns everything back to you in upper case.
That's what how that works.
And step seven it says, why do you think dot lower and dot upper might be important when you are checking if conditions are True or False.
So if we take a look at this first condition we've got if name is equal to Anakin now in this condition Anakin is written in lower case.
So if the user entered Anakin in upper case, or with the first letter as a capital this condition wouldn't be True, even though they'd still written that word Anakin in there.
So that dot lower is there as part of data validation to make sure that what's been entered is accurate.
And it's going to work with our condition later on.
So it's quite a useful tool for us to use.
Then he says line six and seven contain an else and a print statement so there we go else and Print.
Does the condition name is equal to allocate, need to be True or False for these lines of code to be executed and again I could type it in I could type Anakin in lower case to see if this worked or not.
So if I just run it in, if I type Anakin in lower case whoops.
Well doesn't matter now cause we've got that dot lower in there.
He says, how do you do Anakin? So it's running this line of code here, but it's not running this line of code, is it? Because it's going straight to this part, which is line eight.
So if I put something different in if I put Rebecca, it says, nice name, Rebecca.
So Rebecca is it hot or cold? So now it is running this line of code.
So the condition needs to be False in order for line six and seven to be executed, then he says lines 10 to 12.
So these ones here, lets just have a look.
It's probably that one as well I would have thought contain an if, elif statement, if the user enters cold to the weather question, what will be the output on the screen directly after so cold in lower case.
So if they put that and they put it in lower case, it's going to convert it to upper.
And then this condition says whether is equal to cold and it's checking for in upper case.
So if they type cold, it's going to say, you must be freezing so I could type it in again.
Whoops oh I cannot spell Anakin is it hot or cold? If I put it in lower case, you must be freezing.
Do you like the colour blue? It says the next one.
If the user enters hot to the question, what will be output on the screen directly after? So if they put hot, then this will be False.
So this line of code here is going to be executed.
So if it's equal to hot, it's going to say drink plenty of water.
So I'm going to say is it hot? So I'm going to put hot and it says, drink plenty of water.
Then it says lines 14 to 15, contain a else statement.
So 14 and 15, there we go.
I just make this a bit larger.
You can see the whole thing in one guy.
So 14 and 15 that contain an out statement.
What does the user need to enter for it to output? I can't advise you on that type of weather.
So if this is True, then it's going to say this.
If this is True, it's going to say this, but anything else, it's going to say this line of code.
And let's just double check that.
I can't advise you on that type of weather.
So if they type anything else, then those lines of code are going to be executed.
Then it says lines 18 and 19 contain the code if likes blue, print I like blue too.
It says, what does the user need to enter for I like Blue too to be output.
So they need to enter Yes but if we look at this input of both there, they need to enter it with a capital Y and then lower case E-S so it has to be entered exactly as it is, okay.
So we're going to run it again now I'm going to go, she's going to test at that work.
So Rebecca, and then we're going to go to cold and we're going to put Yes with a capital Y and then E-S and it says have a good day and it says bye, so it does say I like blue too there, because I've put Yes with a capital Y at the beginning.
Then it's got step 13 what is displayed on the screen if the user types yes in lower case or yes in capital letters or anything else.
So if they type anything else, then it's going to, so if I put yes in all capitals, then it's just going to say, have a good day, Bye.
It's not going to say I like blue too.
Then you've got your modified task for your modify task it says, adapt the code online 17 and 18 so that the input is converted to upper case and this is checked in the condition.
So if I go here, if I go to upper case here or type, Yes.
And then I've also got to put that bit on the end that says dot upper and put dot upper like that.
And now if I type in lower case, yes.
Then it's going to work, let's have a look.
So do you like the colour blue? If I put in lower case yes this time is going to say I like blue too.
So adding that extra little bit in end converted it to that Capital, Y-E-S and now my condition is working.
And then the next one says at line six, introducing any branch that checks if the name is leia.
If the name is leia then the message should display.
The force is with you.
So instead of having this, else at line six, I want to put in my elif, so elif, and then what was it again? It says, if elif branch it checks, if the name is leia if the name is leia, then the message should display, the force is with you.
So if name is equal to, and I'll put it in lower case, because if we look above, it's converting it to lower, I just go spell it correctly leia.
Then it's got to say, the force is with you.
So the force is with you.
And then I can just double check that the programme actually works so I can put leia in lower case.
The force is with you, okay.
So that's one working.
And then the next one says, using else with the final, if statement.
So where's the final if statement there it is and it says if likes is likes blue is equal to Yes.
If the user doesn't answer Yes when asked if they like blue, then the programme should output.
That's a shame because I really like blue.
So I just need to add my else here.
And I can just go above and just look at how that's been written.
So it's all lower case with a colon.
So else like that and then you got to print.
That's a shame because I really like blue, Like, so, and then I'm just going to run it again and to test it.
So I've got leia hot put red.
That's a shame because I really like blue.
So I know the is working and that is the end of the whole task.
So you might have got most a bit, or you might have got all of it or you've, might've struggled a little bit.
So if you did struggle a little bit, then feel free to just go back over the video with me and just do the coding along with me.
That's absolutely fine.
Let's have a go it's a multiple choice questions then.
So here's a question for you when this programme is executed, what will be displayed on the screen if the user enters Python with a capital P at the prompt.
So read that code and see if you can figure out what the correct answer is.
Pause the video while you do that.
Great let's look at the answer then.
So the answer was, Hello Pythonista.
So well done if you've got that right.
Let's have a look at this one now and he says, when this programme is executed, what will we display on the screen if the user enters a zero at the prompt.
Let's take a look at the answer then.
The answer is zero is positive.
Let's look at the next one then.
So he has a new, multiple choice question.
It says when this programme is executed, what will be displayed on the screen so read it carefully, and see if you can make a prediction about that.
Pause the video.
Excellent so the answer is, medium.
Okay, so thats the end of this lesson, you've learned a little bit about selection and you've learned about if, elif and else and how using branches can change the flow of programme execution.
If you'd like to please ask your parent or care to share your work on Instagram, Facebook, or Twitter, tagging at OakNational and hashtag LearnWithOak, cause it'd be great to get in at two in this lesson.
And I'll see you again soon for lesson four.