# Lesson video

In progress...

Hi, I'm Rebecca, your computing teacher.

Now, if you found lesson one a little bit tricky, then congratulate yourself for coming back and having a go at lesson two.

If you found it quite easy, then maybe make a promise to yourself this lesson to push yourself a little bit further and maybe experiment a little bit with the code.

We can do this together.

We'll get there.

Now this lesson, you're probably going to want to have a pen and paper nearby just to make some notes if you need to.

You're also going to need that Replit account that you set up in lesson one.

Now make sure you do get your parent or carer's permission to use Replit.

And then don't forget those distractions.

Try and remove as many distractions as you possibly can so that you can really focus this lesson.

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

In this lesson, you will use arithmetic expressions, use variables in programmes, and write programmes that receive numerical input.

Let's start by making a prediction.

Have a look at this programme on the left-hand side.

You've got lucky equals 13, and then you've got print, "my lucky number is" lucky.

And what I want you to do is have a think about which might be the correct answer.

It says what will be the output of print when this programme is executed? So take a look at those responses and see if you can think about what might be the correct answer there.

Pause if you need to.

Let's take a look then.

So it was my lucky number is 13.

Now, why was it 13 and not lucky? Why was it? It's because we've got our variable lucky, and what we've done is we've referred to that variable in our print function, and that's why it's displayed what's being held in the variable and not the name of the variable.

So this programme will always produce the same output whenever it is executed.

Is that true? Or is it false? What do you think? Let's have a look.

It's true.

So we've got no input on there at all, so there's no option for the user to enter any data and have that tailed in a variable.

So this is going to do exactly the same thing every time it's executed.

Let's make another prediction then.

So this time we've got print, "my lucky number is" lucky, and then we've got lucky equals 13.

And the question is what will be the output of print when this programme is executed? And you've got four different options there to take a look at and see what you think.

So, what do you think will be the output when this programme is executed? Take a look now, pause if you need to.

Right, let's have a look at the answer then.

So there is an error in the programme.

Did you spot the error? During programme execution, a variable must have been assigned a value before that value is referenced.

And if you take a look at this programme, the variable is assigned after it is referenced.

So line two is where the variable is initialised and assigned, so that's when that variable is created and it's given that value 13, but before that, we've referred to it, we referred to lucky in that function, so it would cause an error message, because it won't know what lucky is because it doesn't exist at that line of code.

So let's look at this third question then.

So make a prediction now, we've got print "What's your name?" We've got user and we've got an input, and we've got print "Hello" user.

So what will be the output of print when this programme is executed? Take a look at those options, pause if you need to to think about the answer.

Let's take a look then.

So this is the correct answer.

So hello and whatever the user typed on the keyboard, because we don't know what they're going to type, so we can't put, you know, it was Claude or Rebecca, because we don't know what they're going to type, so we just put in whatever the user typed on the keyboard when they got the input there.

Now this programme will always produce the same output whenever it is executed.

Is that true, or is that false? It's false.

It's false because we don't know what the user's going to enter.

They could enter anything, so it's always going to be different depending on whatever the user has typed in.

Lesson one, we started looking at assignments and it's important to know that assignments are not equations, even though you might think that they look like them from your math lessons.

This assignment does not mean that the day's variable will always equal 365.

Assignments are instructions to be executed, so not equations.

This is an instruction to assign the value 365 to the days variable.

A subsequent assignment can assign a new value to the days variable, replacing the previous value.

And if you've used Scratch before, you'll have seen assignment in a block like the one that's on your screen.

You can use expressions in assignments.

So if you take a look at what has been highlighted there in the left-hand side in the programme, you can see an expression, and this is something that you've probably seen before in your math lessons, where it's an expression that you need to solve, and you usually put the answer or the evaluation in the right-hand side.

It's very different in programming.

This is an instruction to evaluate the expression on the right, and then assign the value to the days variable on the left, so it does it the opposite way around, and it's an instruction.

The tip is to read assignments from right to left.

You'll have seen assignments before if you've used Scratch, and you've got there a variable, and we're setting that variable to the evaluation of the expression.

You can use a huge range of arithmetic expressions in Python, but it's important to know that it does use slightly different symbols that you might not be used to.

And don't worry, you don't need to remember all of these right now, because I will be showing you them as and when you need them.

But you can take a look at them now if you want to.

The ones that probably stand out the most are the multiplication symbol being an asterisk.

You might think that's a little bit strange if you haven't used spreadsheets before as well, because that's often what's used in spreadsheets for multiply.

And then your division symbol is very different, because it's a forward slash.

But if you haven't, that's going to look a bit strange to you.

But they are just all the same expressions that you'll do in your math lessons, but just using slightly different symbols that Python understands and can interpret.

An expression can refer to the values of variables.

So that first expression on line one of this programme doesn't refer to any variables.

It just contains numbers.

But if you look at line two, that's been highlighted for you, you will see that, within that expression, there is a variable name.

To evaluate this expression, the days variable must have been assigned a value.

So in this situation it has, because days has been assigned on the first line and then it's been referred to in the second line.

During programme execution, a variable must have been assigned before that value is referred to.

The machine executes the code in the order that it is written.

So let's take a look at this programme and see what happens.

So on line one, it evaluates the expression first.

So that was our expression that we were looking at earlier and evaluate the expression, it means it finds out what will happen when this expression is calculated.

It assigns that value then to the variable day.

Now, if you didn't realise, it's calculating the days of the year there, so we've got seven lots of 31 days, seven lots of months that have got 31 days, and then there's four months that are 30 days, and then there's one month that is 28 days, so the grand total of that is 365.

And then at that point, the state of the variable days is 365.

So let's look at the next line of code.

So first of all, it's going to evaluate the expression, 'cause that's instruction that it's being given.

And the expression now includes that variable days as well, so that value 365 that has been assigned to days is used.

And the total, the answer is 1,461.

And then that is assigned to quad, so the evaluation is assigned to the variable.

And then we can see the state of the variable quad is 1,461.

And then we look at our final line of code, print quad "days in four years".

What do you think the output is going to be? Have a little think, make a prediction.

Let's take a look.

So the output is going to be for 1,461 days in four years.

So it's taken what has been held in the variable quad, and it's displayed that using the print function with the rest of the string, so days in four years.

Now you're going to be given a worksheet to have a go at very soon.

So before you do that, I'm just going to show you a little tip on how to do step two, because you might not have done this before, So it's good to have a little demonstration to see how it actually works.

On step two of your worksheet, it's going to say access the original file using this link.

And on there, it's giving you a short link, and that short link will take you to Replit, and it'll give you a starter programme for you to use that you'll need to edit.

And I'm just going to show you how to do that now.

Be able to see the top left-hand corner that it's looking at to the file that's on the NCC account.

But if I go in and I start modifying it, so if I press Delete on that one, then what it does is it takes it to my account.

Can you notice there it swapped it now to Frank's Brie pie? So that's my Replit account now, and now this is mine and it's saved in my files.

So now I can edit it as much as I like and look at that seven K back as well, so the original file comes over.

So when you need to modify this programme, as soon as you start modifying it, it should take you straight to your own account, just because you've been logged into it already.

But if it doesn't, then you'll be able to sign in and save it that way.

What I'd like you to do then now is find the worksheet that comes with this lesson and it's called Order Matters.

So the worksheet comes in two sections, and the first section is Order Matters, and that's the section that I want you to do now.

So it says complete the first part of the worksheet called Order Matters.

You will be asked to investigate a programme that assigns and refers to variables.

So using everything that I've just talked about in the last few sections, you're going to be using that and applying that using the worksheet.

So have a go and I'll go through the solutions with you afterwards, so pause the video now and come back when you're done.

How did you find that? Is it a little bit tricky? It should've been a little bit challenging, but hopefully not too challenging, but let's go through the answers now.

So that task one, at step one, it asked you to look at that programme code and then see which of those sentences the lines of code applied to.

So let's see what you got for those.

So the first one, A, it says assigns a value to the secs variable.

So let's see which line of code that happens on.

Did you get it right? Number three.

So on number three, you've got the assignment for secs, and then it's assigned to the value 2,567.

And then B says assigns a value to the mins variable.

So where's that happening? It is line two.

Okay.

So on that programme there, it is assigned at line two because you can see there, we've got mins equals secs integer division 60.

So that's what's happening there.

The evaluation of the expression is being assigned to mins.

And then we've got C refers to the value of the secs variable.

So where's it being referred to? Let's have a look.

It's happening in one and two, so you might have just said one there, but it's actually happening in two as well.

Or you might have just said two and not realised it was in one, but it's actually being referred to in both.

And then D is refers to the value of the mins variable.

So where's it being referred to? Let's have a look.

That's in line one, so it's not referred to anywhere else, just in line one there.

So hopefully you got those right.

Maybe you got maybe one or two wrong, but that's okay.

We're all learning here.

It's okay to make little mistakes as we go along.

So let's have a look at the next one.

So this was the solution to that Parsons puzzle that you were given.

If you notice that programme there was in the wrong order.

The one that you were given at the beginning, because it was referring to variables before they'd been assigned.

'Cause if you look at line one, you're referring to variables before assignment has happened.

And then the solution to change it all around was to make sure you had it in the right order, whereas you actually turned it so line one, two, and three became lines three, two, and one.

So it was completely opposite way around to get the correct answer, because you needed to do the secs variable first, that had to be assigned first because it's being referred to in the mins variable in that expression there.

And then both of them are being referred to in that third line.

So that's why it had to be in that order.

Here are some subtle points when we're looking at the order in which our instructions are executed.

So here's a question for you.

What will be the value of double after executing line three? So take a look at that variable double, take a look at the whole programme, those whole three lines, and decide what value you think is going to be held in double at line three.

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

Fantastic.

And some learners get a little bit confused and think it's 30, and that's quite a common misconception for learners to come up with, and you might have done that as well.

Now, the reason that learners might think that is because they look at number 15 and they do two times 15, because they think it must be that because number is 15.

But actually these instructions are carried out in the order that they are written in.

So line one will happen first, so let's look at it.

So number is assigned five initially, and then line two will be executed next.

And that is double is assigned two times number, and the value held in number is five, so two times five is 10.

So at that point, double holds the value 10.

And then the next line of code, number is assigned 15.

So we haven't reassigned anything for double at that point.

It's just number is assigned 15, so double will still hold the value 10.

Let's look at this one now then.

We've got what will be the value of number after executing line two? So take a look.

You've got two lines of code there, and I want you to think about what will happen to the value of number after executing line two.

So take a look at those answers and see which one you think it is.

Pause the video now while you do that.

Great, so let's see what the answer was.

So let's just take a look at the order in which this is happening.

So line one, number is assigned the value five, and then on line two, number is assigned the current value of number, which is five, plus 10.

So that means that number is assigned 15.

'Cause essentially you're saying on that second line that number is going to be assigned five plus 10, so the answer is 15.

The expression number plus 10 is evaluated, and the result is assigned to number.

The previous value of number is replaced.

And it's important to know that it's replaced.

You might've seen something like this in Scratch before where you've set a variable to a number and then you've also added an extra little bit after you've incremented it in some way.

We're going to have a go now at creating a programme that calculates the age from the year of birth.

So I'm going to do a live coding session, and you can watch the demonstration, then you can have a go at it, or you can code along with me if you find that easier to do it that way.

But let's have a go and see if we can do this programme.

And then you need to create a new Python file.

So we go all the way over to here and we choose Python from the list.

And again, it always gives us a silly name to use, but we want to make sure we use a decent name.

We are doing an age calculator, so I'm going to put agecalculator like that, and then I'll go to Create Repl, and it's going to take me.

Now, remember I've got it in dark mode, but you might want it in light mode.

You want might want smaller text, it's up to you.

You can change that in your settings.

So here is my development environment now so that I can programme in Python.

Now what I'm going to do is I'm going to create an age calculator, and what that's going to do is going to ask for the user's year of birth, and then it's going to use that year of birth to calculate their age based on the current year, and then it's going to display the age back to the user.

So to get this started, I'm going to need to display a message to the user so that they can enter their year of birth.

So if I want to display output for the user, what function am I going to need? I'm going to need the print function.

So let's have a go.

So print, what is your birth year? Or what year were you born? It's up to you.

When you do this, you can put your own question.

You can edit it and make it different if you want to.

And if you look, I've got the structure right there, I hope.

So I've got print in lower case, I've got my two brackets, and I've used my quotes either side, and I can run the programme to test that it works.

And it does.

It says what is your birth year there.

Now on my next line of code, I want to actually receive the input.

So I'm going to have to prompt, have my input prompt and whatever they type in the keyboard, I want that to be held in a year variable.

So how am I going to do that? I'm going to have to start off by naming a variable and I'm going to name it birth_year.

And then I'm assigning it, so I'm going to use that single equal sign.

And then what function do I need to get something from the keyboard from the user? I need the input function.

So input, remembering both of those brackets.

So again, I can test it and see what happens.

So now I've got what is your birth year? And then I've got a new line and it's waiting for a keyboard prompt.

So I'm going to type in 2000, 'cause that's not my year of birth, but I don't want anyone to know my age, so I'm going to put 2000, and then I'm going to press the Enter key, and the programme has ended and it hasn't thrown up any error messages at this point, so it seems to be working.

So let's have a go.

Let's look at the next line of code.

So I've got my birth year, and now what I've got to do is work out the age from that birth year.

And the way to do that is take the current year that you're in and then take away your birth year and then you'll be left with your age.

So we're going to be left with the age, so it makes sense to call that variable age, and then we've got to think what our expression is.

Now I'm currently in the year 2020, but you might be somewhere in the future in a different year, so if you're in a different year, you're going to have to put your current year that you are in, but I'm going to put 2020.

And then I've got to take away my birth year.

So that is being held in the variable birth_year, like so, and then I can take that away from 2020.

Now I'm going to run my code and see if it works this time.

Let's just see what happens.

So I'm running it.

It's got what is your birth year? Yeah, that's working fine, so I'm going to put 2000.

And then let's see what happens next.

Oh dear, we've got an error.

So the dreaded error that we get, the syntax error.

Now, I knew that was going to happen.

You might've known as well, but let's see what the error message is, and see if we can fix it.

Now, let's look at that final line.

It says type error unsupported operand type for int and string.

And what is happening here is we've got an expression, and that expression needs to contain the same data types.

Now at the moment, this has got an integer data type, which is a whole number, and he's also got a data of the data type string.

And the reason that this has been entered a string is because we've used the input function.

The input function always returns string values.

So actually, birth_year, when I execute this code, is actually equal to 2000 like that, which is string.

That's what it's returned.

So that's not going to help us, is it? Because it's getting confused, 'cause you can't have an integer and then take away string from that integer.

It's not going to work, it's getting confused.

So what we have to do is we have to make sure that the input is an integer and not string.

So remember the default is string.

It'll always return a string value, that input function.

But what we can do now is you can wrap around it another function, and it's called the int function.

So if I wrap around it the int function.

If you take a look now, so I've got int open bracket and then I close bracket, so I've wrapped it around that whole function.

What it'll do is it will return it a string, like it's meant to, 'cause that's what the input function does.

It will return that 2000 a string, but then the int function will return that value as an integer value, and then it can be used in my calculation.

So let's see if it works now.

So again, year of birth is 2000, and we've not seen any error messages, but now we can't see what the calculation is, because we haven't displayed that to the user at the end.

So what do I need now to display this all to the user? What function? I need a print function.

So I'm going to go to the print function again, and I'm going to put you are, and then I have to move across, because I've got that speech mark in order to put the age.

So now I want to refer to the age variable, so I've put a comma and then I've put age, and then I'm going to put another comma to finish the sentence off with the string.

So age you are, whatever it is, years old like that.

So let's see what it does now.

So I'm going to put 2000 in there.

So it's saying you are 20 years old.

How nice would that be to be 20 again for me? So yeah, it looks like it's working now and it's displaying it correctly.

There's been no error message and it's working.

So you're going to have a go at this yourselves now.

So pause the video to create your own age calculator.

And if you do need to, and you want to just follow along, then just go back, skip back to where I'm doing it, and just copy it along as I do it as well.

Try and do it from memory if you can, but then if you get stuck along the way, just go back to the video and copy it, and it will help giving you those practise, typing in the code, trying to avoid syntax errors and things like that.

So don't worry if you're struggling with it a little bit from memory, just go back and copy me, that's fine.

So pause the video and when you're done, come back.

I've now got two challenges for you.

Now, these challenges are fairly similar to the programme that you've done already, but you just need to adapt them and think of the different expressions that you might need to do to complete those.

So the first one is what do you weigh on the moon? Your science teacher asks you to make a programme that reads the user's weight on earth and calculates how much the user will weigh on the moon.

You do some research and find out that gravity on the moon is a sixth of what it is on earth.

So what I want you to do, first of all, is come up with a calculator to calculate your weight on the moon.

And this is all on the worksheet.

And then the other one is to calculate your age in dog years.

So two new programmes to have a go at, but they do follow a similar structure to the one that we've just done in the practise.

So I want you to use the second part of that worksheet now and take a look through.

There's some instructions to help you a little bit along the way, but this is for you to have a bit of a challenge to see if you can do it.

So see if you can have a go, have a look back at the programme that we've just done, 'cause that's going to give you some clues about what you've got to do as well.

And please don't worry if you struggle a little bit.

It's completely normal to struggle a bit and to get some syntax errors and maybe get the order wrong.

That's the whole process of learning.

This is the journey that we're on.

So if you do find it a little tricky, don't worry.

I'm going to go through the answers later.

But really do try, use the worksheet, look back at your old work to try and see if you can do by yourself.

So have a go at those tasks on the worksheet now.

So how did you find that challenge? That's your first proper challenge there, so let's do a thumbs up or thumbs down, like maybe you're somewhere in the middle.

How did you find it? It's good if you found it a little bit challenging.

It should always be a little bit challenging, but not so hard that you want to give up.

That's kind of how I try to pitch things in these lessons.

So hopefully you found it a little bit challenging but not too challenging, but don't worry if you did.

I'm going to go through the answers now and talk you through them.

So hopefully if you did struggle a little bit, hopefully this will shed some light on what you needed to do.

So that first one was that moon solution.

And if you look at this code now, and you compare it to the age calculator, you'll see that we've actually got four lines of code just like we did with the age calculator.

We've got a question for the user at the beginning.

Instead of birth year, we've got your weight on earth, and then you've got weight_earth, a variable, and it's accepting integer input.

Remember, we need, because we want to do a calculation with it, we've got to make sure we do that integer function wrapping around the input function so that it takes that string value and brings back an integer value for us.

And then we've got an expression.

So we've got the weight on the moon and we've got an expression there for your weight on earth divided by six.

So it was that forward slash for a six.

So I did point that out to you at the beginning.

I gave you that little sneaky introduction to what that symbol was, so hopefully you remembered.

It's that forward slash six, and then print.

The weight on the moon is whatever that calculation, the evaluation from that calculation there.

So it was a very similar structure.

It's just for a different purpose and a different expression.

The only thing that's really massively different there is the expression that's on line three, because it's just doing a weight divided by six rather than a takeaway expression.

So that was that one.

And then we've got dog years.

So how'd you get on with this one? So dog years, again, very similar structure to our one in the example at the beginning.

So you've got a question for the user.

How old are you? You've asked for them to enter it, and you've held that value in the age variable.

You've wrapped around the integer function to make sure that the value returned is an integer.

And then you've got dog_years, and you've got another arithmetic expression there.

So it's age times seven because hopefully, you know that dog years are worked out by your human age times seven.

So this time we were using that asterisk symbol, so hopefully you remember that from earlier too.

And then we've just got displaying it back to the user.

So print you are dog_years years old in dog years.

So hopefully you can see that there's a bit of a pattern there with those three programmes.

Those three programmes do different things, but they follow a very, very similar structure.

So you've got a question, you've got some input, you've got an expression, and then you've got some output.

They all follow the same structure, but just for different things.

So I really hope that you've tried really hard today and that you've found it a little bit challenging but not too challenging, and that you're raring to go with lesson three now, and you're getting a little bit more confident.

That's what I would hope at this point.

So 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 getting up to this lesson.

And I look forward to seeing you next lesson.

Bye-bye for now.