video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher for the programming part three unit.

Now this is part three.

You should already have a really good understanding of sequence and selection before starting this unit.

And if you're unsure about those things, then you can take programming part one and programming part two before starting this unit.

I'll still be here when you're ready, but it's very important that you make sure that you have a go at those two before starting this unit.

For this unit, you'll also going to need a Repl.

it account and you should ask your parent or carer's permission before setting up a Repl.

it account.

You're also going to need a pen and paper in this lesson so that you can make notes and also so that you can write down the answers to any questions that I give you in the lesson too.

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

In this lesson, you will define iteration as a group of instructions that are repeatedly executed and you'll modify a programme to incorporate a while loop.

What I'd like you to do first of all then is make a prediction about this programme.

And it does contain some completely new code that you won't have seen before.

So your question is, can you predict what the outcome of running this programme will be? And you've got some hints.

So your first hint, you've got a tip there, it says while block.

These statements are to be repeated.

You've then got while condition.

This condition is checked at the beginning of each loop.

And then you've got a little flowchart as well to help you sort of visualise how this might work.

So pause the video while you have a think about that.

So the answer then, this programme will keep asking the user for their name until the name Hedy, at which point it will display a greeting.

And this is an example of iteration.

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

And this is, as you've just seen, an example of iteration, and this is a while loop.

We've got there while, in there you have some kind of condition and then you have a block of statements underneath.

Let's do a step by step execution of this programme that we just had a look at.

First of all, you've got print, What's your name? So that's going to display, What's your name as output to the user.

You've then got name equals input.

So it's asking for some user input.

So the user types, Margaret, and then that value is then held in the variable name.

We then move to the next one where a condition is checked.

So it's saying name is not equal to Hedy.

So is that True or False at this point? Let's take a look.

It's True.

Then we go to the next line.

Because it was True so a little bit like with our if statements, if that condition is True, then the blocks of code underneath are going to execute.

So print, Try again Hedy is going to be displayed on the screen, and then it's going to ask for the name again and put in some input.

So if they type Ada, for example, then that will be held in name.

And then we go back to that condition and we check if it's True or False.

So is it True or False at this point? It is True at the moment, because name is still not equal to Hedy.

So it runs those blocks of code again.

So, Try again Hedy and then asking for input and then user types Hedy this time.

So then we've got Hedy held in the variable name, and then we go back to that condition at the top and we've got there name not equal to Hedy.

Is that True or False now? It's False.

So just like with our if statement, those blocks of code that are indented are not executed and it just goes straight to the next part, the next line of code that's in the main block.

And it'll just output, Hello, Hedy.

So here's some subtle points then.

Here's a question for you.

So what difference would it make if the line in pink is removed, do you think? Pause the video while you have a think about that.

So if that line in pink is removed, there's no initial value for name, an error will occur when checking the condition in while.

So it'll say it's not defined.

Here's another one then.

What difference will it make if the line in pink is removed? So assume the first name is not Hedy that's entered.

Pause the video while you have a think about that.

So here we go.

We've got the value for name is not modified and the programme will never terminate.

And that's a tricky one for some learners to get their heads around it.

When you start doing your own while loops, you'll probably forget to do that a couple of times, you'll be like, why is this not working? What's going on? And it's most likely because you haven't changed something like an input or a variable in there.

So then that condition at the top always is True, so it keeps running that loop.

Now we're going to add iteration to a guess the number game.

So here's some start code and now we're going to go into Repl.

it and I'm going to show you how we can take that guess the number game, and then put in some iteration, so incorporate a while loop.

So let's go and take a look in Repl.

it.

Now, if you've done programming part one and part two, then you'll already know how to use Repl.

it.

But if you don't, we've got here our Repl.

it account.

So you can sign up by typing in R-E-P-L dot I-T.

And you can set up your new account with your parent or carer's permission.

And then when you're logged in, you'll be able to go to your homepage.

This is where I'm at now.

And then any Repl.

its that you've done in the past, you can find in here, it saves them in the cloud automatically for you.

And then if you want to make a new Python file, then you can always see a plus sign in the top right-hand corner.

And I'm going to click on that now.

Now the last programming language I used was Python.

So that's going to be at the top there, and then I can come up with a sensible name for it.

So I'm practising.

In fact, now I'm going to do a guess the number game.

So I'm going to call it guessnumber, and then I'm going to go Create.

Now I've got my settings set up so that text is huge.

If we just go to Settings here, text is Huge and theme is Dark.

And that's just because I find that easier to read that way.

But if you're not happy with your settings, then you can always change yours.

So I've got this start code.

This is where you type your code.

And then when you run your code, it will be executed and the output will be displayed over in this right-hand pane.

So here's our code that we're starting off with.

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

So there you've got number equals four.

You've got print, Guess a number between 1 and 10.

So it's going to display that.

Then it says, guess, and it's asking for integer input.

So they make their guess and then you've just got an if statement there, if guess not equal to number print Incorrect else print Correct.

So can you make a prediction about what this programme is actually going to do when it's executed? So just pause the video while you have a think about that.

Let's take a look then.

Let's run it and see what happens.

So I'm going to run it.

Guess a number between 1 and 10.

And I'm going to put eight.

And it says Incorrect and the programme terminates at the end.

If I run it again, if I type in a four this time, it says Correct and the programme terminates.

So you only get one guess with this programme.

Now I might think, well, I want to allow the user to have multiple guesses.

So one way to do that is to just take those blocks of code that are there and I could just repeat those blocks of code.

I could just add in.

Say, I wanted to give them three guesses, I could just repeat those blocks of code three types.

So I can put, I can get it wrong.

Oops, I don't want to get it right.

I want to get it wrong.

And then I want to get it wrong again.

And then I'm going to get it right on the last one.

And I've got it correct at the end.

But this doesn't help if you're not able to predict how many guesses they're going to make it in.

They might get it right in the first guess, they might get it right in the 10th guess.

So you can't predict what they're going to do.

So finding, but doing this, repeating those statements by just copying and pasting the code is not a sensible way of doing it.

Now, this is why we use while loops.

While loops allow us to repeat a block of code until a condition becomes False.

So if we look at that again, we've got while True, do something.

And then when that loop terminates, we want it to do something else, okay? That is our basic structure for our while statement.

So while this condition is True, do these block of statements and then once that's terminated, I want it to do this block of statements.

And what I can do is I can apply this structure now to what I've got here.

So I could literally just take that if and change it to a while, just as a starting point.

So I've got here while guess not equal to number, print Incorrect.

Now, if I did this, it's not going to quite work as correctly as also I've got this code on here, which isn't real code, this is pseudocode, which you'll learn about more later on in this unit, but it's the starting point.

So I've got my while statement, I've got my condition.

So while guess is not equal to number, print Incorrect, but this bit is wrong.

So I'm going to get rid of this bit.

I'm just going to move it down here 'cause what do we want to happen when they are correct? Pause the video while you have a think about that.

When they are correct, we want it to say that they're correct.

And this line of code here, just like we've got here, do something else, just realised I spelled that wrong.

So this do something else, we want it to print, Correct once they've got it correct.

So I can actually keep that line of code.

I just don't need this else anymore.

Like so, and I'm going to get rid of that, all right? So now let's just run the programme and see what happens.

In fact, before I press Run, pause the video and just see if you can think about what might happen.

Let's run it and see what actually happens then.

So I'm going to run it.

It says, Guess a number between 1 and 10 and I'm going to get it wrong.

So I'm going to put eight.

Oh dear.

So what it's doing is it's now repeating that block of code over and over again.

And why do you think it's repeating that block of code over and over again? Have a think about that.

Let's just take a look at the condition though.

So you've got while guess is not equal to number, print Incorrect.

Now, if we look, we enter that number to guess in line three.

So I put my number eight in there.

So this is going to always be, so if number is four and the guess is eight, this is always going to be True.

We are never giving it an opportunity to become False.

So now we've got stuck in a loop, so I've got to fix the code now.

So how do you think I can fix the code? To fix the code, what I can do is I can have that variable assignment again.

So if I have guess equals int input again, and that allows me to put a different guess in there and then we can make sure that at some point that condition will be True and then the loop will break and it'll execute this code below it.

So I could put some instructions in there as well, if I wanted to, but I'm just going to copy and paste those instructions there.

Copy and paste, there we go.

And let's run it now and see what happens.

So, Guess a number between 1 and 10.

I'm going to get it wrong.

Now it says, Incorrect and it says, Guess a number between 1 and 10.

I'm going to get it wrong again, So Incorrect.

Guess a number between 1 and 10, I'm going to put one, Incorrect.

Guess a number between 1 and 10, I'm going to get it right this time.

And now the loop is terminated because this condition is now False.

So it doesn't execute these statements within this block and it just goes straight to that line there.

Now you're going to have a go at doing this too as well in a few moments.

So you'll be able to get a practise at doing this.

And then you're going to do a series of tasks to expand it a little bit further as well.

What I'd like you to do next then is use the Guess the Number section of your worksheet to extend the Guess a number game and practise using while loops.

So pause the video now while you have a go at that.

Excellent, so let's take a look at how you could have completed those tasks then in Repl.

it.

I'm going to go through them with you now.

So what I'm going to do now then is I'm going to take you through how I would have solved that worksheet, so the tasks on that worksheet.

And I've got the worksheet down here, which I'll be reading off to show you and what you can do as you go along, if you got a bit stuck as you were doing the tasks, then you can see the ways around it.

Or if you did it all perfectly and you got it all right straight away, then you can just check that off for yourself and you think oh yeah, I did that right, and that's how I did it.

So either way, it's up to you, you find your learning.

You're all going to be at different stages.

You're learning from home as well, which isn't easy.

So don't worry if you didn't get things right straight away, I'm here to help you.

So first of all then you are asked to introduce a variable to count the guesses and you have the little checklist for you to follow.

So the first thing on the checklist said, initialise the variable guesses first at the top of your programme.

So I'm going to put it here.

Guesses, and then I'm going to initialise it at zero because they haven't had a guess yet.

Decide what line of code will increment the variable by one after each guess.

So what I'm going to do is I'm going to decide, let me think.

So if they, hm they will have made a guess here, so they'll have one guess at that point.

So would we maybe increment it here? And this is just me thinking.

I haven't tested this programme yet.

I'm just thinking out loud.

So I'm going to do it like this for now.

I might change it later.

So guesses equals guesses plus one, and that's going to increment the guesses variable.

So they started with one and then if they've made one guess, then it will come here.

The only thing is it might affect it if they get it right straight away.

So I've got a feeling this isn't going to be quite right, but we can always come back to that later.

So then it says place the line of code in the most appropriate place, that's where I think it should go at the moment.

Test your new code by printing the variable to see if it increments after each new guess.

So this is a good little trick is to just put in a print statement there to show, to display on the screen what is happening inside that variable, so that you can just keep track of what's going on.

So I've got, Guess a number between 1 and 10, I'm going to get it wrong.

And it says Incorrect.

And you can see that the number one has appeared there.

And so that seems to be working fine, right.

Then it says, next one, it says task three, display the number of guesses.

So you've got the checklist again.

It says at the end of the game, it should reveal the number of guesses made by the user.

So let me think.

It's got some helpful texts there 'cause I've got that input/output table on the worksheet as well, which you should have already looked at.

So it's saying it should look like this.

You guessed it in a certain number of attempts.

So I'm going to use an f string so that I can display that string nice and neatly within it.

So, all right, let me just look at it.

What does it say? You guessed it in, and then it wants the guesses.

So I've the curly brackets, guesses, and then attempts and then the speech marks at the end.

So I can just run that now and test it again.

So guess the number, I'm going to guess it wrong, and then I'm going to get it right.

You guessed it in 1 attempts.

Well I didn't do that, I did it in two.

So there's something not right there.

So do I need to have this set to one? Let's just have a look.

So Guess a number between 1 and 10, I'm going to do five again and I'm going to do four.

You guessed it in 2 attempts.

So that seems to be working now.

I'm just going to get it right first time though and just double-check.

So if I put a four in straight away, You guessed it in 1 attempts.

Now the grammar is a little bit wrong because it's saying attempts, which is, there's only one attempt.

It might be better actually to reword that and just have number of guesses and then a column.

I might just do that now actually.

Number of guesses and then have it like that 'cause I think that will make more sense.

So if I get it right the first time, Number of guesses: 1, and it's Correct.

And then I can have, get it wrong and then get it right.

Number of guesses: 2.

See that looks a little bit nicer as well.

So I'm happy with that.

The worksheet said to do it a slightly different way, but it doesn't matter.

It's my programme, I can do what I want.

Now task four, it says only allow three guesses.

Now this is a tricky one, isn't it? So you've got, currently your programme allow an infinite number of guesses and it does, doesn't it? So if I run this, I can put nine, eight, one, two, eight again, seven, zero.

Can put all of those guesses in as many times as I like.

And then it just displays the number of guesses there and it says, it's correct.

So it then says, this needs to now be limited to just three guesses.

So we've got to think about the condition that is up here and what we need to change for it.

It says change the condition on the while loop so that it only continues to the next iteration if they haven't guessed correctly and their number of guesses is less than four.

So now we can link back to what we were learning in programming part two, where we were looking at logical expressions and we would think about And and OR and all of those things.

And it does say AND in bold, so I think I've got to use an AND here.

So if guess is not equal to number and guesses is less than.

If it needs to allow, only allow three guesses, I'm going to have to have it as less than four, or I could have less than or equal to three, couldn't I? So let's just, yeah, let's just run it now and see what happens.

So I'm going to get it wrong once, so eight.

I'm going to get it wrong twice, and then I'm going to get it wrong a third time and it let me guess it again.

So that hasn't quite worked, has it? It said, Number of guesses: 4 and then I've got it correct.

Oh, there's something wrong with this bit as well.

Let's not worry about that bit yet.

So it's letting me do it for four guesses, so there's obviously something not quite right here.

And I can trace this in my head.

So let's just see if I can figure this out.

I'm going to just trace it without using a table.

So guesses starts off at one, then guess isn't equal to number and it's less than four, okay? So we've got guesses equals guesses plus one, so it's two, and then they type in their next one and it's, then you've got number of guesses is two, and then it's going to do it again.

And it's number of guesses is three.

Ah, I see what's happening.

So what's happening is because it increments there at this point, it still allows it to be four.

So actually I think I need to just change this to three.

'Cause it's implementing that increment after the condition, it can be a three and then it runs it and increments it to four to that fourth guess.

So now hopefully it should work.

Let's just see.

So I'm going to get it wrong, I'm going to get it wrong, I'm going to get it wrong, and I did it in three guesses, but obviously I'm not correct, but my programme is still saying I'm correct, but let's just get it right.

So wrong, wrong, right.

Number of guesses: 3.

Okay, so we're almost there.

And then it says to think about, so task five says, displaying the correct message at the end of the game.

So if you look at what it says now, it says correct even when you're correct, even whether you're correct or not.

So we've got to think now, well what can I do at the end to display the correct message? 'Cause at the moment it's just saying Correct.

So I can have actually an if statement, another if statement.

So if number is equal to guess, then it should say Correct, else it should say Incorrect.

So that's that bit that we were missing.

So I'm going to run it now, I'm going to get it wrong, wrong, right.

And it says Number of guesses: 3, Correct.

And I'm going to get it wrong this time.

So eight, eight, eight.

And it's saying I'm incorrect.

So now the programme is properly working.

Now you did have that optional extra, that extension task to look at including random.

And again, this was something that was covered in part two at the beginning of programming part two was randomization.

So instead of having the number four there, so it's always the number four, what I can do is I can introduce random.

So I can say from random import randint oops, randint, and then instead of having number equals four, I can have number equals randint one to 10.

And then just to help me with testing, I can print number there.

I'm going to get rid of this one as well 'cause I don't need this anymore.

So I'm going to do Run now.

So it's saying that the number is five.

So I'm going to get it wrong.

Eight, I'm going to get it wrong.

One, and then I'm going to get it right, which is five.

So number in guesses and I got it correct.

And I should run it again.

You can see I've got a different random number this time and if I get it right then it's correct.

So that was just something extra that you could have tried as well.

So for the next activity, you will be completing a Parson's puzzle.

A Parson's puzzle works by giving you all of the bits of code that you need to solve a problem but in the wrong order.

Your job is to put the lines of code into the correct order.

Use the Parson's puzzle section of your worksheet to solve the puzzle.

Pause the video while you have a go at that.

Brilliant, here's the solution then.

So you can pause the video while you check your programme with this programme here.

Fantastic, so you've now done your first while loop and hopefully you've got a deeper understanding now of iteration and how you can introduce this into your programmes.

If you'd like to, please ask your parent or carer to share your work on Instagram, Facebook or Twitter, tagging @OakNational and #LearnwithOak.

And I'll see you again soon for lesson two.