video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher.

And look at you rocketing all the way to lesson four.

Well done.

Now this lesson, you're going to need that Replika account that you should have set up with your parent or carers permission.

You're going to need a pen and paper to make any notes that you need to.

And you're also going to need to make sure that any distractions are removed out the ways best as you can so that you can really concentrate.

Once you've done all of that, we can begin.

In this lesson, you will explore how selection can handle more than two possible branches, and you can use iteration or while loops to allow the flow of programme execution to include loops.

For your first challenge what I'd like you to do is answer this question.

It says, how would you extend this programme to give advice to the user on how to dress, depending on the weather? Provide a rough outline of what your Python code would look like and give advice for when the weather is cloudy, rainy or snowy.

In other cases display a generic message.

So really think about it, take a look at the stock code that you've got there on the left-hand side and think, well, what did you learn in lesson three that might help you extend this programme so that it gives different responses based on certain conditions.

So pause the video and think about that.

In lesson three, we started to look at selection and this is the type of selection that we looked at.

So these versions of selection, check one condition and select one out of two brunches to follow.

But what if there are more than two cases in the problem? What if there are multiple branches and think about that whether the example that you just took a look at and thinking about the outline that you did, was to give advice for when the weather is cloudy, rainy, or snowy.

Now you probably realised when you were doing the outline that actually there's different advice needed for those different situations.

So you might have thought, "Well, how on earth do I put more brunches in?" Maybe you put several, if statements, maybe you put a few, if else statements, maybe you put them all into one condition and just had one response for those things and one response for other things.

At this point, there's no right or wrong answer but what we are going to start looking at is using more than one branch or multiple branches.

So a multi-branch selection could be if, elif and else.

And this checks successive conditions and checks one of a multiple branches to follow.

So we've got this example here.

We've got, if this condition is true then do this block of statements else.

If this condition is true, do this block of statements else do this block of statements.

So that's added an extra branch there.

You will need an if block with elif block.

We're now moving to two mutually exclusive paths for your programme to follow.

You can use multiple elif blocks.

So you can use many, many, many of those as as you go in along alone.

But the else block, and it is always optional.

And you always have just one at the end.

You can nest anything inside the block statements even more if statements, if you wanted to.

So we're going to look at this strange weather programme.

So we've got our start of our programme there on the left-hand side, but we're going to extend this programme using elif to give advice to the user on how to dress depending on the weather and give advice for the weather, whether it's cloudy, rainy, or snowy.

So you'll have already thought a little bit about this but now we're going to introduce the elif to see if we can get it to work even better.

And in any other case it's going to display this generic message as well.

So we're going to live code this now in Replika.

Let's take a look at what this code actually does.

So I'm going to run it and see what happens.

So, first of all, it's saying, where do you live? And I live in Birmingham.

Hopefully you can tell that from my accent.

So, where do you live? Birmingham.

And then, what's the weather in Birmingham now? And it is cloudy out there.

So I'm going to write cloudy and then the programme terminates because there's no more code layer left to execute.

So at the moment, it's a pretty boring programme.

It's not giving me any advice about what to wear.

And the starter activity was asking us how to extend it so that it did give advice based on what the weather was today.

So what I want to do now is start introducing those if, elif, else, statements.

So I now have to do an if statement.

So I'm just going to do if at the beginning, and then I've got to put my condition.

So we know that we're going to be looking at the variable weather because that's where my answer to the weather is going to be entered.

So we're going to be looking at weather and we want to know if the weather is cloudy, because then we want to give a certain bit of advice about that.

So weather is equal to, so how many equal signs is that? It's two equal signs, so is equal to and then we're going to type, cloudy.

What have I missed? It needs to have the speech marks.

Doesn't it? So, I have the speech marks and then what do I need at the end? I need a colon.

So if weather is equal to cloudy then the advice is going to be- So we're going to have a new variable.

The advice is going to be no sunglasses.

And the reason that it's no sunglasses is because if it's a cloudy day you shouldn't really be wearing sunglasses 'cause it's going to be very dark.

Isn't it? So that's my first one.

That's my first bit of selection.

And now I'm going to start using my elif.

Now, if you remember last time when we wanted to put that L sin.

If you look the cursor goes to the next line and it's indented.

So you have to remember to press that back space.

So I'm going to do the same thing again now, elif and I'm going to think of a different condition.

So I'm still looking at that variable, but I'm thinking of a different weather type that I want to compare.

So weather, how many equal signs? Two equal signs.

Do I need speech marks? Yes.

So if weather is equal to rainy what do I need at the end? I need a colon.

So if it's equal to raining, I'm going to have take an umbrella.

Is that how we spell umbrella? No, it isn't, that should take that up.

I always struggle with the word umbrella for some reason.

So, no call on this one.

And then we're going to have another elif statement for the other option.

Now, again, when you press on to key, is going to be indented.

So you have to press back so that you go all the way over to the left-hand side.

So then we've got our other elif and our other condition and this one is going to be for snowy.

So what condition have I got to write there now? I've got to do weather.

Whoops, is equal to snowy.

And then remember my colon at the end.

And then my advice is going to be, wear a hat and a big coat.

And we don't have a colon at the end of that one we just leave it like that.

Now where I'm from, you call it your big coat is your coat that keeps you really warm and it's snowy.

I don't know where you're from.

You might call it something different, like a thermal coat or something like that.

I call it a big coat.

Now the final one was just going to be the generic bit of advice.

So if they don't type in cloudy, rainy, or snowy then it just gives them a generic piece of advice.

So this is just with our else statement and then with this no condition for the else 'cause it's just saying otherwise do this.

So in this situation, we just say that there's no particular advice at this time.

Now, if I run this programme, is it going to display any advice to the user? Let's run it and see what happens.

So I'm going to run it and I'm going to type Birmingham and I'm going to type cloudy and the programme has ended it hasn't given me that piece of advice.

So why do you think it hasn't given me that piece of advice yet? It hasn't given me that piece of advice because I haven't displayed it as output to the user at the end of the programme.

So I've got to make sure at the very end I have print and then I can print the variable advice and then it should prim whatever's been held in the variable advice.

So let's try it now.

So run it again and Birmingham, cloudy, no sunglasses.

And just like with everything, we test every scenario just to make sure it works.

So we've got cloudy.

We've got whoops, no comp rainy in there.

That's not Birmingham.

Rainy, take an umbrella.

So that's working and then I'll run it again.

Birmingham and snowy.

Wear a hat and a big coat.

And then where do you live? Birmingham and what's the weather right now? Let's say it's sunny.

And then we have no particular advice.

So now we've got elif statement it's got multiple branches.

So it's giving lots of tailored feedback now, it's getting a bit more specific with the feedback.

And then we've got that generic bit of advice in case the user doesn't type in those three options.

So you're going to try this yourself now.

Pause the video while you have a go at making your strange by the programme.

We're now going to take a look at a people in space programme that actually, when it runs it tells you how many people are currently in space right now.

Let's take a look.

Here is the people in space programme in red plate.

And you'll notice that at the top there we've got this from an import statement at the very top which is a little bit like when we import it from the module random.

So we, from the module random, we imported the function brand it.

Well, this one's slightly different because this module here was written by our team.

So it's not one of those modules that's automatically available in Python.

It's been written by somebody else.

So, there's not to do with the main pipe and programme.

And it's going to be important in that way.

So this will only work in this replica environment where this folder is actually saved.

So what it's doing, you've got a module called ncc.

space and it's important the function people from that module.

And if you take a look at here, it's got a variable number and then it's got this function called people.

And then it's going to re return the number of people that are currently in space.

And It's got print that are numbers.

So whatever's in that variable people in space now.

So let's run it to see how many people are in space right now.

So right now on this day, there are three people in space right now.

So if you were to run this on a different day, maybe in a few weeks time, there's different launches that happened quite a lot that actually might increase or it might even decrease as well.

So this is a module that's been written by someone and you can import it into a programme to run it just like those other modules that are already installed into Python.

And you're going to have a go at using this now, just like in lesson three, you're going to be given a worksheet and it's going to have lots of support there to help make sure that you can achieve the best that you can while completing this worksheet.

So we'll start off by showing you a worked example.

And this is the example code that we use at the beginning of the lesson, where we're talking about what's the weather like and using those elifs.

So if you forget about the structure and how to structure in elif statement, you can go back and you can take a look at the work example and use that to guide you through the actual task you've then got some syntax errors, popular common syntax errors that you might find when you're doing your programming.

So if you do get some of those and you're not quite sure how to fix them then you can go back to this slide and it'll give you some help with that.

The checklist it goes through to just make sure that you've hit all those boxes and that you have, you know how to fix those syntax errors that you've got.

And then you've got obviously testing really, really important testing all those scenarios.

So if you've got three elifs, make sure that you test it for each condition that is on that statement that you've got, then you've got your task.

So you using this people in space programme to extend it further.

So you've got an introduction and explaining how it actually works.

And then you've got your task is to open it and then extend it so that it asks the user to guess the number of people currently in space.

So you've got it.

It's telling you how many people are in space but you know, you're going to adapt it to be something different.

And you've got this table just like in lesson three giving you example, input and output so that you can see how the programme would work in real life.

And then you've got to try and decide what's the code behind this, actually happening.

And then it gives you some tips as well.

So if you're unsure about exactly what you have to do and you can read these tips here to give you a little bit more support, and then there's some further steps to extend it further.

So it's all just about learning how to actually use the worksheet properly and knowing where the support is.

So remember that the support at the beginning with the worked examples, the syntax checklists and telling how to test, but also for a while too you've got this example input and output that you can use to help understand what the programme is actually meant to be doing so that you can start thinking about the code that's happening behind it.

Pause the video now, while you work through the people in space worksheet.

How did it get on then? Where were you? Somewhere in the middle possibly maybe a little bit higher maybe getting a little bit more confident now.

Well, let's take a look at the solution.

So first of all, you'll have that first part of the programme where it's got retrieved the number of number of people from space.

And that was the code that was already given to you at the beginning.

Then you've got this next bit where we're prompting the user to make a guess.

So hopefully you thought about giving the user a prompt and using a variable called guess and then holding in that the integer input.

Remember that integer because you wouldn't have understood it otherwise.

So you needed the integer wrapped around the input because the input function always returns a string and then the int wrapped around it would make sure that it returned, took that string and returned the integer for that, that's been typed in.

And then the next bit is going to be, check the answer and provide feedback.

So it's seeing what they've typed in and it's got that elif statement as well.

So that there's another condition in there to be checked as well as the, if condition.

And then the final one is to display the number of people in space the end.

So again, it's quite similar structure to other programmes that you've done before.

Like the weather programme that you did it follows a similar structure, doesn't it? And this is the thing with programming.

You'll find that the more and more examples and the more programmes that you do, you'll start to notice that there's a bit of a pattern with it.

And that programmes that you've made before you can use that structure again to programme something completely different.

So it's good.

The more practise that you do, the more that you'll build this bank of mental models of how things can be solved.

So let's look at this then now, we've got something missing again, like we did before.

So this programme greets the user by name, but what if we want you to repeat the process until a specific name has entered? Many of our programmes have sections that could be repeated but at the moment, they're not 'cause they just literally follow through it.

They don't want to at the the end of it.

Checking the weather for multiple locations, for example and a number guessing game with many, many guesses rather than just one chance of getting it right.

This is where something called iteration comes in or loops or repetition.

The word that we tend to use is iteration to describe it but it just means repeating or loop.

So when your programmes, repeat actions checking for a terminating condition at the beginning of each new loop.

And you might have seen loops before in scratch.

So if you take a look here, we've got repeat until loop.

So something will have happened until a condition was met.

And then the other one is the forever loop that you've probably remember, the forever loop just carries on forever and ever until somebody terminates the programme by properly stopping it in some way.

Now, just like last time when we have the selection statement we've got similar things that we can do in Python but this time we have to type the syntax in ourselves rather than just dragging the blocks in.

So in Python we have a while loop.

So it's not repeated until there is a while loop.

And it works slightly opposite as well to the repeat until loops.

You'll get this when you start doing it yourself.

So while the condition is true It'll keep running those block of statements.

It will keep repeating those block of statements.

Whereas with the repeat until the loop it'll keep running that until it's true.

Whereas the other one it's wireless too.

So it's a little bit tricky to get your head around but I'm sure you will, as we do a bit more practise.

So you all loop is while the condition is true run this block of statements.

And then you can also just have just like with your forever loop you can literally just put while true.

And then that will loop it forever.

So it won't wait for a condition to become false to break or to terminate.

It will just keep going until somebody actually stops the programme.

Let's take a look at this.

You will need a while loop when your programme needs to repeat actions while a condition is satisfied.

So this is an iterative programme.

Can you predict what the outcome of running this programme will be? So just pause the video now and take a look at the programme and see if you can make a prediction about what will happen when this runs.

Let's take a look.

So, if you weren't sure you've got a little tip there you've got a wild block.

These statements are the ones that are going to be repeated.

So that's your little clue.

You're under the clue.

The wall condition, this condition is checked at the beginning of each loop.

So let's look at the answer.

So this programme will keep asking the user for their name until the name is Hedy.

At which point it will display a greeting.

So let's take a look at that now in a replit.

Here's my programme then, let's run it and see what actually happens.

So if I run it, it says, what's your name? And if I put Rebecca, it says, try again Hedy.

So I'm going to put Paul.

Try again Hedy.

So now if I put Hedy.

Hello, Hedy.

So what It's done is, it's kept that loop going until the condition was false.

So this condition here is name not equal to Hedy.

So every time I didn't enter the value Hedy, that loop kept going round and round and round.

Here are some subtle points then to think about when we are using our world.

Oops, so take a look at the question.

It says, what difference will it make If line two is removed? So line two, there is name equals inputs.

Take a look at the answers the possible answers and pause the video while you think about what the answer might be.

So the answer is this one.

So there's no initial value for name, an error will occur when checking the condition in while.

So just like it has happened before we cannot refer to a variable if it hasn't been assigned already.

So if we would move line two, name we'll have never been assigned.

So technically, it won't exist in our programme.

And then we're trying to refer to something that doesn't exist.

So it's going to cause an error message.

Let's take a look at this one.

So what difference would it make if line five is removed on this one? So think about it, pause the video while you look at those responses and come up with your answer.

Let's take a look at the answer.

So the answer is this one, the value for name is not modified.

The programme will never terminate.

So if we didn't allow for that value name to be changed, while within the loop, then that condition will always remain true because it will still be looking at that initial assignment that happened on line two, 'cause it would have never been reassigned to a new value.

So it'll get stuck in that loop.

So it's important to have it inside as well.

And you'll probably find that you'll make that mistake as you start doing more, lose a bit more, loose a bit more by yourself.

So it's very important to remember that you've got to reassign or assign again, that variables to be different in order for that loop to break.

That's everything for this lesson.

How did you find it? Was it tricky? Was it okay? Hopefully I'm really hoping that your confidence is beginning to build up now and that you're feeling a little bit more comfortable with programming Python, becoming a bit better at spotting those syntax errors, and you're thinking about the sequence a little bit more.

So hopefully that's what you're experiencing and what I'd like you to do, if you can, if you'd like to, please ask your parent or carer to share your work on Instagram, Facebook, or Twitter, tagging @OakNational and #LearnwithOak.

'Cause as always, we would love to see what you've been up to this lesson and I shall see you soon for next lesson.