Loading...
I'm Rebecca, your computing teacher.
Wow, you've made it to lesson five, almost to lesson six.
Well done, you must be really proud of yourself for the progress that you've made so far.
Now, in this lesson, you're going to need the Repl.
it account that you should have set up with your parent or carer's permission, and you're going to need pen and paper to make any notes that you might need to, and also to make sure you remove as many distractions as you possibly can.
Once you've done all of that, we can begin.
In this lesson, you will use iteration or while loops to allow the flow of programme execution to include loops, and you'll use variables as counters.
Let's start by making a prediction.
So have a look at this question.
What will this programme display when it is executed? So pause your video while you have a look at that question.
Got the answer? Let's see what it is.
So it will print three and then two.
Were you right? Let's take a look at why.
So assignments are not equations.
Assignments are instructions to be executed.
This is an instruction to evaluate the expression, count-1, and assign the value back to the variable count, replacing the previous value.
So that's why it does three, and then it does two.
So this is an instruction to decrease the value of count by one.
So let's look at the state of those variables as we go through.
So, first of all, count is equal to three, and then we do print count, and the output is three because that is what is being held at that time.
Then, we've got count = count - 1.
So now, the status of that variable has changed to two because it started at three, and then we've taken away one, which has left us with two.
And then, when we do print count this time, because the state of the variable has changed to two, the output is now two.
So what will this extended programme display when it is executed? Pause your video and just have a think about that now.
Great, so it will do, it will output three, then two, and then one, 'cause we've added that extra little bit now, and it's decreasing by one each time, that's our instruction that we're giving it, and then it's displaying it after it's been decreased.
So that's why it's gone three, two, one.
And you can see that there.
The final one is zero, but we haven't displayed that as output on the screen at this point.
Now, what we can do with variables is we can use something called a trace table.
A trace table allows us to trace the state of the variables at each point in our programme.
And this is what is happening here.
So let's take a look at what is actually happening.
So we've got here, count = 3.
And if you notice in our trace table there, we've got the line number that it's on, we've got the variable name that's happening there, and we've got the output as well that's happening on the screen.
So, at the moment, count is equal to three, but no output because there's no output on that line, and then we've got print and the output is three, and that's on line two, and then line three, count is now two, but there's no output.
And on line four, the output is two because that's the current state of the variable.
For line five, now count is one because we've taken away one again, and on line six, the output is one because that's the state of the variable, and then for line seven, the state of the variable is zero.
Now, what I'd like you to do is take a look at the worksheet, and I want you to use the worksheet to incorporate a while loop into the countdown programme.
And what the worksheet will do is it will step you through.
So remember, we've got all those worked examples, those syntax checklists, we've got our testing as well to support us.
So use all of those things to help you complete the programme.
Pause the video and come back when you're done.
How did you get on? Show me your thumb.
Where did you get to? Hopefully, you were somewhere, just a little bit here.
So it wasn't too challenging, but it was a little bit challenging, hopefully.
That third one was probably the trickiest out of all of them.
So I'm very impressed if you got that one done.
And just remember, if you did struggle a little bit, and you didn't quite get to the end, you've got the solutions there on the screen now.
So what you could do is you could go back, and you could type those instructions into Python, and run them, and test them, and just see if you can get them to work and start to understand them.
So use this screen to help check your work to see if you got it right or not.
So let's take a look then.
Times table practise, a single question.
So what we're going to do now is we're going to start modifying a programme that we've been given here.
So we've got a times table practise programme, and at the moment, it only asks one question.
So it selects a random times table question to ask using that from random import randint that we used a few lessons ago.
So it generates a random times table question, it asks for the solution, and then it uses an if else statement to say whether they were right or whether they were wrong, which is our start point for our code.
So it generates two random integers, it asks a user for the product of a and b, it calculates the correct answer, and then it checks whether the correct answer that it's calculated matches what the user has also entered, and it provides some feedback.
So what I want you to do now is take that times table starter programme and see if you can incorporate a loop into that.
And, again, use that worksheet for your guidance and support to try and help you get to the correct programme.
Wow, how did you find that? Was it a little bit tricky? Did you have a struggle with the indents? Did you get them right? Did you get them wrong? Did you get a few syntax errors when you added in that while loop? Did you get the condition right, for example? What we're going to do now is we're going to take a look at the solution in Python, and we're going to run it through and just to make sure that you understand how it works.
Let's take a look at the solution then.
We've got that import statement at the beginning because we need that so that it generates those random integers for us.
We've then got a variable called questions, and at the beginning, it is set to zero because we haven't asked any questions yet, so the counter is going to start at zero.
And then, our condition says, while questions less than three.
And at the first iteration, this is going to be true because questions is equal to zero.
So while zero's less than three is going to be true, and then these blocks of code here are going to run.
So what it does is it's got two variables, a and b, and it generates two random numbers between 2 and 12 within them, and then it's got questions = questions + 1, And what that is doing is it's adding one because once it's asked that first question, that variable, questions, that at the beginning is set to zero, it needs to increase by one because it's counting how many times a question has been asked.
So before we ask that first question, it's going to increase it by one.
So at the moment, it will be increasing it to one.
And then, it says, print question, and then it says questions.
So now, it's actually going to use that variable to say what the question number is.
So it's going to say question one, the first iteration.
And then, it's looking at those variables, so it's got the first variable times the second variable equals, and then it's waiting for an answer.
And this was probably one of the ones that you got stuck with.
Remember, we've got to do an integer input.
So we've got to make sure that that input function has int wrapped around it to ensure that it's returning an integer.
And then, it works out the answer itself so that it can check whether it's the correct answer or not, and then it checks it here in this if statement below.
So let's just have a run and see what happens.
So we've got question one, five times four equals, and then if I get the answer right, which is 20.
Well, I hope that's right.
Let's just see.
It says, yep, that is correct, and then it moves on to question two.
So you can see if we were looking at the state of those variables, the first time it run, the very beginning it's zero, but before it asks the question, before it prints it on the screen, that question state has increased by one to one.
So it's saying question one.
Then, it's got those random integers that it's selected.
So five times four, and then you put the answer.
And then, this part of the programme at the bottom is checking whether it's correct or not.
And then, it's given me my next one.
So the next one, so 99.
Let's see if I'm right.
Yeah, that is correct.
Question three, 11 times 11.
And I'm going to intentionally get this wrong.
It's not because I don't know the answer really.
So I'm going to put 56, and it says, I am sorry.
So that means I got it wrong.
11 times 11 is 121.
So you can see, it asked me three questions, one, two, three, and then once it had asked me those three questions, the programme terminated because it had got to the end of the programme.
So you might have got all the way there with your solution.
You might have got it perfectly right.
But if you didn't, it's worth taking a look at this.
The link is on, if you go back on the video, you can see the link for this.
So you can see the solution, and you can have a play around with it yourself to see exactly how it works.
So it's worth doing that if you did struggle with coming up with the final solution.
Let's do some code reading.
What I'd like you to do is take a look at these two programmes here.
It says the only difference between these two programmes is the condition in the while loop.
When does each while loop terminate? So pause the video and have a think about that.
When is the loop going to terminate? Right, let's take a look.
So it's always going to terminate at the condition.
So if you remember, the condition runs the code within it.
So the while loop will run the code within it when that condition is true.
So when it goes back to the top, and if it's false, the loop will terminate, and it'll go back to the main programme and carry on with the next part of the programme.
So the loop always terminates at the condition.
It's a very important key thing to remember.
Well done.
You've worked so, so hard this lesson.
I think you've done brilliantly, and you're really, really doing well.
We've only got one lesson left in this unit.
How far have you come with your Python skills? I hope you've done extremely well, and your confidence is getting much, much better.
And 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 we'd love to see what you've been up to.
See you again for the final lesson in this unit.