video

Lesson video

In progress...

Loading...

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

For this lesson, you're going to need a replica camp which you should have already set up with your parents or carers permission.

You're also going to need a pen and paper to answer any of the questions that I give you in this lesson and to make notes, if you need to.

It's also a really good idea to try to remove as many distractions as you possibly can so that you can really focus in this lesson.

Once you're ready, we can begin.

In this lesson, you will define a for-loop, you'll walk through code that uses a for loop.

You'll modify a programme that uses a for loop and you'll compare a while loop and a for loop.

Now this is a brand new piece of code that you won't have seen before.

So don't worry if you can't make a completely accurate prediction with all these prediction questions.

It's just about trying to get you to re code and try and figure them out.

It's all part of the learning experience.

So what I want you to do is think about what will the output be when this code is executed.

So pause the video while you have a good look and see if you can make a prediction.

Let's take a look then.

So the values zero to four will be output on the screen.

Is that what you had? It's a tricky one isn't it? This is an example of a for loop.

A for loop is another tool that we have to control the flow of execution in our programmes.

A while loop can do everything that a for loop cam however, it is more convenient to use a four loop for iterating through sequences.

When we use a four loop we are saying, for every element in this sequence do this, for loops can be used for many types of sequence.

For example, the letters in a word the items in a list and a list is something we'll look up much further later on and members in a range.

This lesson will focus on for loops that use the range function.

Range is a sequence that we're going to iterate through.

Range is a built in function, just like input.

It generates a sequence of numbers.

When we call the range function we can pass it up to three values, the start number the end number and the step.

If we only pass one value then the function we'll use it as a last number.

And the sequence will start at zero.

When we call the range function it will generate a sequence of numbers.

So zero one, two, three, four and note this is very important that the end number is not included in the generated sequence because it is used as the stop point.

X is a variable that is used to iterate through this sequence.

Let's take a little walk through them.

The range function will generate a sequence of numbers and you can see those on the right-hand side zero, one, two.

X will hold the first value in the sequence.

So you can see that X is zero and then the volume held in X is displayed as output.

X holds the next value in the sequence next.

So if you look, it's now moved along that sequence to one, and the value held in X is displayed it's output.

X holds a next value in the sequence, which is two.

And you can see there the state of the variable is two and then the value held in X is displayed as output.

The end of the sequence is reached.

So the loop terminates.

And here's some example code that is being used with different amounts of values being passed.

So if you have a look at the first one, if we pass one value and it will be used at the end point.

Pass two values, there will be used as the start and end point.

And pass three values and they will be used as the start, end and the increment or step value.

So that is a little bit like how run range works with our randomization.

If you think about back to part two lesson one, when you learned about randomization.

That's a little bit how the run range function works as well.

You've got the start, the stop or the the minimum and the maximum, and then the step.

So there's some similar similarities there.

Now you'll get to take a look at using this yourself and you're going to use the times table generator section of your worksheet to explore an extent at times table generator.

So pause the video while you work through that worksheet.

Excellent then.

Let's take a look at the solutions then.

So I'm going to go through what you could have done for this so that you can either see if you're correct or you can get a little bit support if you needed it.

So I'm going to go through that worksheet with you now.

So I've got it here with me in front of me, and I'm going to go through what I would have put for the answers so that you can check whether you were right, but also if you were struggling with some of those questions then hopefully I'll help point you in the right direction.

So, first of all, you were asked to make a prediction and then run your programme see if anything surprised you about it.

So if it did, then that's completely normal.

You might not have got it completely accurate.

This is the first time you really started looking at for loops.

So don't worry if you didn't get it as accurate as you as you would have liked.

So I'm just going to run this programme now.

This what it should look like for you as well.

So let's just run it and see what happens.

So it says, here is the five times table.

One times five is five, two times five is 10.

So it goes all the way through the times table there.

And it just displays it as output on the screen.

They had some investigates steps to see if we can figure out how it actually works and start to learn a little bit more about it.

So let's look at, look at those steps.

So step one said, execute the code.

What is the second line that is output for displays? So the second line is one times five is five.

And it says what is the last line that is output for display? And it is 10 times five is 50.

Then it says, line six is use to output the times table message on the screen in the first iteration, what is the value of X? So here we go we've got line six is used to output the timetable message on the screen.

So this is this message that appears here in the first iteration, what is the value of X? So if we look here, we can see that it starts at once.

If you do remember it about how for loop works, that is used to the stop point, but also you can see what that value is because it's displayed here.

If you look you've got X there, you've got X there, X and X there it's all holding that same value at that point.

And then it's displaying it as output there.

So we've got one.

So it's one.

So that's the value.

Step three says investigate the variable times tables where's that so starts off at five and it says, does the value of time sailor change throughout the running of the code? So if we look here, we've got timestamp was five.

So that's the first time it appears.

And it says, here is the five times table.

And then when it goes, here we go, X times, times table.

And then it's it displays it out put their times table.

And then if we look over here, it's always a five, isn't it? So it doesn't change throughout the execution of the code.

Step four then says investigate the variable answer.

So there's the variable answer.

And it's initialised as zero.

What are, which metallic expression is assigned to answer? Well, first of all, when it's initialised it's not assigned in every arithmetic expression it's just assign the value zero but is there an arithmetic expression somewhere else? Lett's take a look.

So if I go down here now, answer is assigned an arithmetic expression.

So it's timesing the value of X with the value of times tables.

It's always timesing it by five, but this X is always changing.

Isn't it? Because it's incrementing.

There's what happens to the value of X after each iteration.

So I answered my question there is iterating through isn't it.

So each time we look at the value of X here, you can see it goes up.

We know that that's displaying X, the value of X and it's going up by one each time, because it is iterating through this sequence.

And then in the final output, what is the value of X? So if we look here, the value of X is 10 in that final output.

'Cause you can see it there.

Then if we look at step seven, it says line four has the code range one to 11, there we go.

So there's that bit of code.

Change, the values two range two and 22.

Okay, so I'm going to do two and then 22 what is the second line that is put four display? So, let's have a look.

So the second line is now saying two times five is 10.

So it starting at two this time and it says, what is the final line that is output? Well, it's 21 times five is 105.

So if you look there, we put 22 is that final value, but that is used as a stock value.

Isn't it? So it always goes one below there.

It says change the range to 10 and 30.

So I'm going to do it again.

10 and 30.

What is the second line? Is output for display? So here we go.

So it starts at 10 and it ends at 29.

So how do you think the value X relate to the value past in range? So what it's doing is it's incrementing from 10 all the way to 29.

So this is the stock value and this is a stock value so we, always increments and then it stops at this final value.

So that's when the loop terminates.

That's why we get warm below, When we look at this final number.

And it happens every single time.

So, if you can put in any numbers in there.

So I could put, a hundred in there.

And what do you think the times table is going to go to? Is it going to go to a hundred or is it going to go to 99? Let's run it and see what happens.

It's going to go very long there.

There we go.

It's going to go to 99 because that is the stock value.

And that is not the final value that is used.

So, that's something you need to be aware of because that's a common misconception that learners get confused with sometimes.

So the next thing was to modify.

So, let's take a look at those modified steps now.

So it says change, the value passed in range, so that the times table generator will output the five times table from one to 12.

So, keeping in mind that we've got a stop value, we know we've got to have it at one, but we don't put 12 there.

What do we put? We put 13, so I'm going to put 13, and I'm going to run it to make sure it works.

And it says, the first line should do one times five is five and the final line should be 12 times five is 60.

So I know I've definitely done it right.

Then it says introduce an input to the programme, to allow the user to enter the times table that they wish to create.

So, you can do it however you like for this.

Think about the instructions that you might need to use and how that's going to work.

But I'm just going to put here.

Now, I'm going to make sure it's integer input because we want it to be seen as a whole number.

Like so, now I've done that wrong.

There we go.

And it's missing a fine of bracket.

So I've done that, I might just put a print at the top, as well as some instruction.

So which, times table would you like? There we go.

And now when I run it, if I put a two it should hopefully do the two times table, and it does.

So that's quite cool.

So it says test your code and I've just done that.

And then finally it says, let's have a look.

So you might have made this mistake.

You might have not included the int.

And if you didn't include the int there, then let's just see what would have happened.

It does it like this, doesn't it? Because remember that you can't mix those two.

So, instead of doing two times two in terms of numbers, it's doing it in terms of strings.

So it's just repeating that same value over and over again.

So if you've got something like that, it's just because you didn't use the int, you to make sure you wrap that around it.

So that's something you might've come across.

Then the next one says, add some additional functionality to your code by allowing the use to enter the value that they wish the time-saver to go work to.

So we've got a few steps here.

There's quite a lot to go through it.

So to create a variable called max value, assign a value to max value through user input.

So I'm going to put max value and I'm going to put int input like so.

And it says, add a message prior to the max value input to let your user know that they need what they need to enter.

So, what is the maximum value you would like? And then replace the 13 in the range with max value and test your code.

So hover up, there we go.

Max value.

Test your code.

Let's see what happens.

So I've got, which timestamp would you like? I'm going to do the 10 times table.

What is the max value you would like, a hundred.

And it's got all the way up but this time it's gone to 99, not a hundred.

So, we've got a little error here a little bit of a logical error going on.

We've got to figure out how we're going to fix that.

So it says you will notice that the maximum value entered is not reached when this code is run because of how the range function works directly below the code where the max value is input and to some code that will increase the value by one and test your code.

So, I'm going to have max value equals max value plus one and then I'm going to do the same thing again.

So 10 times 10, and this time it has gone to 10.

Another way I could have done it is I could have put plus one in there, but it's not as neat and tidy to do it like that so sometimes better to do it there.

It's up to you.

You'll find your own style as you start to learn a bit more about programming.

And then finally it says to use the, try and except a data validation tool to make sure that only an integer is entered, which we can do.

Let's just have a look.

So, I've got to make sure that I have try and then I have an indent.

So try that except, value, error and then print, you must enter a number and then put that same line of code again.

Like, so, and if I run it now it should not allow me to put some text.

You must enter a number.

So now, that has just checked, whether I've entered text or number, and it's given me another opportunity to put that incorrectly.

And that was the end of the activity.

You now going to have a go extending that programme to turn it into a times table quiz.

So use the times table quiz section of your worksheet to create a quiz.

Super.

So we're going to look at the solutions now.

So let's take a look at how you could solve this problem.

I'm going to go through that worksheet there now.

And I've got that worksheet here with me.

So that's why I keep looking down.

And I'm going to show you how I would have solved the problem.

And hopefully it'll either prove you correct, or it might help support you if you struggled a little bit with some of those points.

So first of all, it wants me to make sure that I'm using the programme that I'd already started with in the last activity.

So I've got it here.

And I also added in some extra validation there for the second input.

So that's ready to go.

It also suggests that you save a copy.

Now, the way that I figured out to save a copy in *rec plate is to literally copy and paste it into a new file.

You might find another way of doing it but that seems to be the easiest way for me to do it.

So I'm just going to take all of that.

And I'm going to go to python, and I am going to do times table quiz create ripple, and then I'm going to paste my code in like so, and now I've got a copy.

So now I am ready to go with the actual worksheet and it wants me to make sure that the user types in what time table they would like and the max value they want to go to, which you've actually already got there on the screen.

And it also then wants you to test it.

So it'll come up.

I don't know, five times five is, and then the output in the answer and then it will say whether they were correct or not, this is the programme that we're working towards.

So, save the copy.

And then it says, set two.

This is what I'm looking at now.

It gives me a little checklist to go through in order to change this into a quiz.

So the first step is modify the welcome message to greet the user to the times table quiz.

So, which times table would you like is not a good greeting message.

So, I'm going to have welcome to the times table quiz.

That'll do for now.

Modify the print statements for the two inputs in reference to the quiz and test your code.

And on the next slide there were some sample input and output as well.

So if you weren't sure what to write for those print statements, you could go on and have a look.

So in the example, it's got one to times timetable quiz, it's pretty much what I've got and it's got enter a times table that you would like to be tested on.

So, instead of this one, enter a times table that you would like to be tested on.

And then it says, they'll type it in.

And then the other one is enter the maximum value for your times tables.

So where is that, here we go.

Enter the maximum value for your times table.

And there we go.

So that's that bit done.

And it does say to test it, so I'm going to just test it.

So enter times table that you would like to be tested on 10, enter the maximum value 10 and it's doing it's through that for loop.

And that's just checked really that my print statements my new ones have added in are working and all the code the other code has pretty much stayed the same.

So then he says per step three, just before the for loop in this line of code.

So just before the for loop, where is that for loop? There it is.

It's gone all the way down here now because we've got all these extra things in this data validation that's going on.

So it says, just before the for loop in this line of code.

So here is the times table, times table.

This one here is the times table, times table.

Rephrase the code to say that they will be tested on their chosen times table.

And then you've got your checklist at the end of the for loop create a variable called, user answer and assign it to an integer and user prompt and test your code.

So what I'm going to do this rephrasing bit first.

Rephrase the code to say that we tested on the chosen times table.

I'm going to look at the example input output to just double-check.

So here is your quiz on the seven times table.

So I'm going to change that to here is your quiz on, and then it'll say the seven times table if that's the one it was doing.

So, I've got that and let's just run it and see what happens.

So if I put in 10, 10, here is your quiz on the 10 times table.

So that little bit of code is now working.

So the next one then, it says at the end of the for loop create a variable called user answer and assign it to integer input.

So at the end of the for loop, getting that means there create a variable called user answer and assign it integer input.

Done that.

How do you use a prompt to ask them for your answer? No, let me just look at the test code again.

So I'm going to put, so answer is why those input output tables are so important in your work sheet, because you can always skip ahead to them and just see what is meant by that.

And that's why they're really, really useful.

So I've got their answer and I'm going to put it like that.

So that's what it's asking me to do.

And then it says, test your code.

So I don't think this is working exactly how it should be at this point in time.

So enter a times however you like 10, enter maximum value for your times table 10.

Here is your quiz on the 10 times table.

One times 10 is 10, so saying the answer at this point in time, if I put 10, it's just going to move on because there's no check to see whether I was right or not.

So actually, I also need to change that bit so that it doesn't say the answer in the question either.

So I'm going to put dot dot, dot two and now it should be working.

So enter a times, table 10 10.

So he was your quiz on the 10 times table.

One times 10 is, and now I can put my answer and now that part of it is working.

And I've tested.

It seems to be going well.

Then step four now, it says under the variable that holds user answer create an if else statement that will check if the user answer is the same as answer.

So, let's just scroll down a little bit.

So it says, where you decide to put it under the variable that holds use the answer.

We need to have an if else statement that will check if the user answer is the same.

So if user answer is equal to answer, it's right.

Print correct, and then else print incorrect.

Let's see if that works or not.

So 10, 10 and then the answer is 10.

We got it correct.

And then if I just run it again and I do 10, 10, and I get it wrong, it says incorrect.

And it's moving all the way through now as well through the whole times table.

So we're actually getting there and then it says, adjust the line of code so that the answer is not revealed to the user.

Now, I've already done that but I haven't, I did it earlier because I just saw ahead.

So you might have done that yourself as well but if you didn't, then it was that part of it is just to get rid of that section and put those three dots and that's, all of it.

They could extend that as well if you wanted to, you could actually put a scoring system in there to count how many they got right.

And then it could display at the end, you got four out of 10 or something like that.

That would be a really interesting way to extend this programme as well if you'd like to.

What I'd like you to do now then is think about how while loop works and how a for loop works.

I now want you to answer these three questions on it.

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

Let's take a look at the answers then.

So the question one, it was write a description for a for loop, and this is a model answer.

So, you might have written something a little bit different but hopefully this'll help you Mark your work.

So a while loop is a form of iteration that is controlled by a condition.

While the condition is True, the loop will continue.

When the condition is False, the loop will break.

Then we have question to write a description for a for loop.

So this is a model answer again.

So a for loop iterates through a sequence it repeats the instructions for the number of items in the sequence.

And then finally you have to compare a while loop and a for loop.

And again, this is just a model answer.

So you will have something probably a little bit different but this will help you mark your work.

So, while loop continues for an indefinite amount of times as it runs until the condition becomes false.

A for loop continues for a definite amount of times because it runs until it reaches the end of the sequence.

It is possible to iterate through a sequence using a while loop, but a for loop is more convenient for this.

Now you should have a really good understanding of how a for loop works and you've also applied it into your own programmes.

And if you'd like to, please ask your parent or care to share your work on Facebook, Twitter or Instagram tagging in at Oak National and hashtag learn with Oak.

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