# Lesson video

In progress...

Hi, I'm Rebecca, your Computing teacher, well done for getting to lesson five, high five for lesson five.

So this lesson, you're going to need your Repl.

it account that you should have already set up with your parent or carer's permission, you're going to need a pen and paper to make some notes if you need to, and also to remove as many distractions as you possibly can, and then we can begin.

In this lesson then, you will use variables to keep track of counts and sums. You'll use iteration to iterate over strings of characters, and you'll develop a guided mini-project that will help you apply what you have learned in this unit.

Let's start then with an activity where we test your code reading skills.

So take a look at this question, and look at the programme and think about what the output will be, when this programme is executed.

Pause the video while you think about it, and then come back.

And there's a tip there for why it was three, and the tip is, read the assignment below as increase the value of sum by the value of number.

So, the important thing that we're looking at there is that line for sum equals sum plus number.

The sum that is can get a little bit confused with this because they don't understand that sum is already holding a value, and we're just taking what's already being held in sum, and we're adding the value of another variable to it.

So let's just walk through that programme, we've got numbers, and there's a list and it's got some items in it.

Then you've got sum and that's assigned the value one.

And then number equals numbers zero.

So that's the value that's being held in that first location in the numbers list.

So two, so number equals two.

And then sum is just basically one add two, because one is being held in sum and two is being held in number, and we're just adding those two values together.

So the output of sum is going to be three.

So if you didn't get it before, hopefully you understand it now.

We've got another bit then, so take a look at this one, what do you think the output this time is going to be when this programme is executed? Pause the video while you have a think about that.

Let's take a look then what the answer is.

So it's 31, there was a lot of addition going on there because you were having to look at the lists above and add that in it too, and based on what was already in the value so.

So, we're going to have a little walk through now, why was it 31? Let's see what happens, so I've made a list called numbers, we've then got another variable called sum and assigned one to it.

And then we've got numbers equals number zero, so, number now holds that first value in the list, which is two.

And then we've got the expression sum plus number, and there's a calculation there and that's assigned and it's three, and that's now assigned to sum.

Then we've got our next line of code.

So now we're looking at the next item in our list, and that value is four, which is also being held in number.

And again, we're looking at this calculation, so sum, plus number this time is going to be, seven, because those are the two values that are held in the variable currently, and then that will be assigned to sum.

And we're just moving on through that list.

So, now looking at index location two, so that value is eight and that is also assigned to the number variable.

And then we do that calculation again with that expression, so sum plus number is now eight plus seven, what's that? That's quite tricky to do in you head when you talk it as well, so what's the answer? It's 15, and then that is now assigned to sum, so it sits there, and then we do our last part of the list.

So we're looking at 16 now, our last value, so number is 16, and then we do that calculation again, so it's sum plus number, so 31, is it 31? Yeah, it's 31, and then it's going to assign that value as 31, and then the final print is that output of 31.

So, that is why it got to 31, there's quite a lot of calculations it's quite a little bit tricky keeping track of it all.

And that's why sometimes it's not so bad if you reading it and not talking, but if you wanted to write it down and draw it out and put the calculations as you go along that's going to make it much easier to track the values that are in those variables.

So what I want you to do, the instructions in the rectangle are repeated for every number in the numbers list, and your task is to see what you can remember from lesson four, and see if you can do a for-loop, so modify the programme so that a for-loop is used to achieve the same result.

So, you might want to look at programmes that you did in the last lesson, to see if you've forgotten how to structure a for-loop.

But this might be some good practise to see if you can do it from memory.

So open up Repl.

it, open up a new Python programme, and see if you can have a go at turning that into a for-loop, pause the video while you do that.

Now that was probably a little bit tricky, 'cause I thought you got a few error messages, or maybe you even got to the output nine, rather than 31 when you were doing this.

'Cause I can imagine you probably did something like this bit of code here, where you kept those two pieces of code, and you put it in a for-loop you thought, "Yeah great, that's going to work." You run it, and you got nine, and you're like "Oh, why is it nine? And then you might have realised, well actually that zero there is an incrementing and it's staying as it's always looking at item zero.

And so it's always looking at two, so that's why it's becoming a nine, and then you might have tried to increment it some way within that code, but actually all you needed to do, was get rid of that line of code, did you spot that? That's quite a tricky one to think about 'cause I tricked you there a little bit by saying that those lines of code need to be repeated, but actually don't, because number is already being incremented within the for-loop, so we don't actually need that line of code that's looking at those new items in the list.

So, if you didn't quite get there, try this code now in your Repl.

it programme and see if you can get it to work now.

Awesome, so.

So just to recap then, you can use for to iterate over anything that has individual elements.

You can use for to iterate over the individual characters of a string as well.

And then there's just a reminder there, so that we can enter a word, and then for each character in that word, you can print the character, and it's going to put each character on a new line as the output.

Now you're going to have a go at doing some practise on strings using the for-loop now.

So use your worksheet, make sure you use those worked examples as well to make sure that you get there okay.

And try your best, read it carefully, have a go and come back when you're done.

Pause the video now.

What you were just given there, was something called a Parsons puzzle you had two of them, and a Parsons puzzle is when you're given all of the lines of code that you need, but they're in completely the wrong order and you've got to put them in the correct order to make the programme work correctly, and also, there were indents as well missing so you had to put those in too.

So you can check that with your work now and see if you got it right to see if you managed to get it working.

And if you didn't, as always, go back and change it and use this solution to help you.

So, that was the first one.

And then the next one, the final one on that char sheet was this one here.

So these are the solutions to the second Parsons puzzle.

So again, check this with your work, see if you were right.

And if you didn't quite get there, you can always go back and try it with this code here.

So how did you get on? Show me your thumbs.

Hopefully you did it all right, we'll see.

So, what you're going to be doing now, is you going to be looking at a mini-project.

So this is where you're going to start to become a little bit more independent with your programming.

So, taking off those reins a little bit so that you can have a go trying doing something yourself.

Now, everything that's on this worksheet, you have coded before, but just for a different solution.

So if you're not sure and you think, "Well, I'm not sure what the syntax is that I've got to use or how good a structure this code." Then all you need to do is go back over your work that you've been doing in this unit, have a look at other programmes that you've done, see if you can find similar things and then you use that structure to help you answer, help you do this project.

And if you really do struggle, then I am going to walk you through the code after this screen here.

So, you can always come to this and then follow and code along with me if you need to, that's absolutely fine.

Don't worry if you have to do that, you still learning this is all very new, you're learning independently on your own, so, you're going to struggle a little bit, you haven't got a teacher right there helping you every step of the way, I'm trying to help you as much as I can.

So if you do struggle, don't worry just come back, and code along with me after this pause screen here.

So, try navigate yourself by looking at all those other programmes that you've done, and pause the video while you do that and come back when you ready.

Well, I'm going to do then now is I'm going to go through how I would have solved the problem that you were given in task one and two.

So I'm going to be doing that in Repl.

it for you.

What I'm going to do then is I'm going to code the solution for you and you can watch me, you can code along with me if you want to, and we can do it together, or you can just watch me to check that what you done is right.

And don't worry if your code is a little bit different to mine, because we're both two different people, we might come up with slightly different ways of doing it, but as long as we solve the problem, then we've done it right.

So don't worry if your code is a little bit different to mine.

So, first of all, you had to have a list of those planets, and you had to have one selected at random.

So, I'm going to start off by making my planets list.

So here we go, and I'm going to start putting the planets in.

Now, when I was at school, I had to remember the order of the planets by using a silly sentence to help remind me.

And it was, "My very early mornings jam, some which usually nauseates people." And the reason I have people at the end is because when I was at school, Pluto was still a planet.

So, and I always remember that in my head forever and ever, because it was just hot, I had to do for my exams in science, and I love Pluto it's one of my favourite planets.

So much so that this is how huge I am, I've even got a hoodie that says, "Never forget about Pluto." So, 'cause I was very upset when it got demoted to a dwarf planet.

So I still remember that rhyme, so I'm going to do that in my head while I do this list.

So Mercury is first 'cause I know that Mars is next to earth, so mercury, my, very, early, or, spell it right, Earth, morning, so that's Mars.

Jam, Jupiter, some which, is Saturn, usually, Uranus nauseates so that's Neptune, and I can't put Pluto on the end because it's not a planet, which is such a shame.

So I've got my list, and I can test that my syntax is right for my list by just printing planets.

And hopefully when I run the programme, it will print my list with no syntax errors, there we go, it's definitely working.

So that's me testing it.

Now, I need to get it to select a planet at random.

And in order to do that, I'm going to have to input the function, randint from the module random.

So from random import randint, and I'm also going to need to assign the random index to a variable.

So, I'm going to call it index.

And then I just need to call the function randint and select one at random.

Now, if you think about it, you've got in your, to get the index location it starts at zero, and it's going to end at seven or eight.

So like, I always just count them just to make sure it's the zero, one, two, three, four, five, six, seven.

So that means I've got to put in zero to seven there, and again, I can just practise it can I see if it works.

So, I'm just going to go to print index, run it.

And there we go, so it's randomly selected Venus.

Let's try again.

So now it's randomly selected zero, one, two, three, four, so Jupiter, and now it's randomly selected Mercury, so it's definitely working.

So that little bit of code now is working too.

Then I've got to have an actual planet held in a variable because at the moment it's just holding the value, the index location and not the actual planet.

So I can have a variable called planet and then I can have planets and then put index there, and then hopefully that should hold, whatever's being held at that index location into my variable, so now I can go print planet and I can test it again, like so, so I'm just going to run it.

And then I can run it again to see if it really is random and again, and say that part is working.

So if I go back to my checklist then it was, randomly selects a planet for the first question.

So, I've done that, that's working.

So the next part was display the name of the randomly selected planet and ask the user for its position in the solar system.

So, in order for me to check that, I'm going to need to know what the position is.

So, I'm going to have another variable called position, and in that, it's going to be the index location, but if I just keep it as the index location, then they're going to need to type a zero for Mercury and I want them to actually type a one for mercury, cause I want it to be a little bit easier.

So I'm going to put index plus one.

So now, I'm just going to print planet, and I'm going to print position and I'm just going to check that that works now.

So it's saying Venus is a planet position two, so it's two away from the Sun.

And it is 'cause Mercury is one away from the Sun and Venus is two away from the Sun.

So that's definitely working now, so I'm happy with that.

I'm going to put those two bits of code together, in fact, I'm going to put those three bits of code together because they seem to be doing the same thing.

So now I need to think of the question and what I'm actually going to write.

So I'm going to have print, I'm going to have question one, and then I'm going to have another print and I'm going to put the actual question so, what is the position off, and then I've got a comma, planet, 'cause I want to use, so if it was Venus, so what is the position of Venus it would say, relative to the Sun? Okay, so, I'm going to test that now and see if that works.

So what is the position of Mars relative to the Sun? So it's working and it's all nice and neat, there's no extra spaces in there, so I'm happy with that.

So I've got my question going, and now I need to actually get some user input so I could have a variable called answer, and it's going to need to check if it's a number as well.

So which function are we going to need to wrap around the input function? It's going to be the int function, isn't it? So I've got int, input like that.

So that's going to get my integer input.

So, hopefully I'm just going to get it to print answer as well, just to double check.

I like doing lots of prints as I go along just to check everything's working.

So, what is the position of Saturn relative to the Sun? Is one, two, three, four, five, six.

And it's just going to display it back to me because I haven't said, I haven't put an if statement in there yet to check whether it was right or not, but it seems to be working, it's getting my number right.

So, step two it displays the name of the randomly selected planet and asks the user for its position in the Solar System, okay? I've done that then.

So the next one then displays a message that informs the user whether or not the answer was correct.

Okay, so now I need my if statement, so I've got to check if the answer was the same as the position, so what do I do? I've got other condition, I've got to check whether the answer is the same as position.

So I have answer is equal to position.

And then I've got to remember my colon there.

And so if it is an.

If answer is equal to position, then they've got it right, so I can have a print that is correct, okay? And then otherwise they're going to be incorrect, so I need to say that is not correct.

So now I've tested it, and now I've got branches as well.

So I've got to make sure that I test both scenarios.

So whether it is right and whether it is wrong, so I'm going to run it, by saying, what is a position of Neptune? So I've got one, two, three, four, five, six, seven, eight.

So it's a position eight, so if I get it right, it says that is correct.

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

So it's asking me Uranus so I'm just going to put one and that this is not correct.

So, I've done the third thing on the checklist.

So let's look at the next one, so it says displays the correct answer even when the user's answer was correct.

See example, so I've got the example in front of me, so it just wants to support.

It needs to display the planet, is the planet number and the position from the Sun.

So, I'm going to put that in my code, let me see.

So, we need to make sure go backwards.

So print the.

in fact no, I'm just going to put planet is, don't want to uppercase that, is, planet number and then position from the Sun.

If it done it right, yeah.

So, now I can check this whole list, I might have done task one now so, what is the position of Mercury? So, Mercury is position one, that is correct.

Mercury is planet number one from the Sun.

Yay, I got it right, so let's just test it for getting it wrong.

So what is the position of Venus? I'm just going to put one again.

This is not correct, Venus is planet number two from the Sun and in that list, it is number two from the Sun.

So if you want to pause now, if you wanted some help with that, you can pause now and have a go at that yourself, or you can carry on watching to look at task two.

The first thing on my planets checklist, my task two, says randomly select a planet for the second question.

And I've already got a block of code that randomly selects a planet.

So I don't need to write that out again.

I can just use that same bit of code again, so I'm just going to copy and paste it.

So copy and paste, it's Ctrl+V to paste, and now I've got my second lot.

Now again, I can practise this and see what happens.

So planet, can test it, sorry, not practise planet and position to see if it comes up with another random one.

We'll see how good the random function actually is, now, let's see.

So what is the position of Mars relative to the Sun? It say Mars is at number four, and then it's selected another one at random, which is Uranus and it's at position seven.

So, I know that my bit of code there is actually working so I can just delete that now, 'cause I've tested it and it is working.

So it's selecting those numbers at random.

So what else it would I have to do, displays the position of the randomly selected planets in the solar system and it asks the planet's name.

So now it is in the example as well, in this example of input outputs there so you can look at that as well to help you.

But what I want to do now is I need to print the question.

So if I look above, I can see I've already done print question one, and then I put the question underneath.

So I want to make it the same.

So print question two, and then I'm going to put the question underneath, which is, what is the name of planet number, position from the Sun? And I keep forgetting to put Sun with a capital S.

Now put a capital there, okay.

So now I can check whether that works.

So I've got my first question will come up and then I'll put in a three, I'm not looking at the question anymore I'm just testing.

And then question two, what is the name of planet number six from the Sun? So it selected that random planet, number six and it's all working.

So now I've got to think about the next one, so I've done that, displays a message that informs a user, whether or not the answer was correct.

So, again, I'm going to have to have some user input on to it, so I can just use the same variable name again, 'cause it'll just reassign the new input into it, so int, is it int this time actually? 'Cause they're going to write the planet this time, so it's not int.

Right, so I'm going to just put input like that, 'cause they're going to put the planet name, and then I've got to do the check.

So it's a local, a new conditioning and it's answer is equal to the planet this time and they are both string aren't they? Because planet is going to have string, and my input is going to have string, so I'm pretty sure that's going to work.

So if answer is equal to planet, and they're correct, what did I put above I put that is correct.

That is correct, and then the otherwise, that is incorrect.

Like so, and now I can test it again to see if it works.

So, what is the position of Mars relative to the Sun? It is, but what is it? Yeah, that is correct.

Mars is planet number four from this Sun.

Question two, what is the name of planet number one from this Sun? It's Mercury, so now I type in mercury making sure that I have a capital M at the beginning.

Yep, that is correct, that's brilliant.

So, that's me done with the section, the raise by the message that informs the user, whether or not the answer is correct, yes I've done that.

And then finally displays the correct answer even when the user's answer was correct, see example.

So, I've just got this display a message just like I've done with this one where I've said the correct answer, I can do exactly the same thing underneath, so print , what we're going to type, planet is planet number, position from the Sun, okay? So let's just test that then and see if it's working.

So what is the position of earth relative to the Sun? Is third rock from the Sun.

That is correct, Earth is planet number three from the Sun.

Question two, what is the name of planet number five from the Sun.

So my very early morning jam, so Jupiter.

That is correct, Jupiter is planet number five from the Sun.

I think I've done it, okay.

Well, congratulate pat on my back for me, I've done it.

I've got there.

So again, you can pause now if you want to, and you can code along and see what I've done and see if you can get that too it in your code, and, it didn't match your code, what you came up with.

Was it about right? Was it similar? You might have done something slightly different and that's fine as long as it's working.

But if you did need a little bit of help, it's fine to just code along with me there.

That's everything for this lesson.

So you've done half of that mini-guided project, 'cause you're going to do the other two tasks in lesson six, where you finish off the programme by adding some more questions and adding the scoring system in.

So, remember to keep that code that you've done this lesson so that you can add to it next lesson.

Well done for trying really hard to doing extremely well, there's only one lesson to go.

I'm very proud of the progress you've made, well done for keep sticking out too, that's really good.

Now, if you'd like to please ask your parent or carer to share your work on Instagram, Facebook or Twitter, tagging @OakNational and hashtag #LearnwithOak, 'cause as always, we'd love to see what you've been up to this lesson and I'll see you seen for lesson six.