video

Lesson video

In progress...

Loading...

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

Now for this lesson, you're going to need the Repl.

it account that you should have already set up in lesson two with your parent or carer's permission.

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

And it's a really good idea to try and remove as many distractions as you can so that you're really ready to learn in this lesson.

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

In this lesson, you will obtain input from the keyboard in a programme, differentiate between the data types: integer, real, Boolean, character, and string.

You'll cast variables by calling a function that will return a new value of the desired data type.

You'll define runtime errors in programmes.

And you'll define validation checks.

Let's start off by making predictions, then.

So what will be the output of print when this programme is executed? Have a good look, pause the video and have a think about the answer.

So let's look at those options.

You've got, My age is age.

My age is 22.

It is not possible to know the output without executing the programme.

And there is an error in the programme.

So let's just focus on those first two A and B my age is age.

So, if you were to just look at this statement you'd probably think actually, yeah, my age is age would make sense as an output but notice that it's got an F string in there.

So it's got F at the beginning, and it's got age with the curly brackets around it.

So it's most likely going to be outputting what is being held in the variable age.

So let's look at the correct answer.

It was B.

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

Do you think that that would be true or false? The answer is true.

It will.

We haven't got any changes there to the programme at all.

We've just got age is 22.

We've initialised that variable with the value 22, and then we've displayed whatever's being held in that variable, which is 22.

So yeah, it's always going to do exactly the same thing.

Let's look at this next one, then.

So what will be the output of print when this programme is executed? Pause the video while you have a think about that.

So what I'd like you to do before you really home in on that answer is just look at the order in which those two lines code have been written.

So you've got there print, My age is age, and then the variable age is initialised.

So is that going to work? Look at those options.

Let's see what it was.

So there's going to be an error in the programme and that's because it was initialised after it'd been referenced.

So that means it's going to produce an error message, because it won't be defined.

Let's look at this one, then.

What will be the output of print when this programming is executed? So age is 22, age is 23, and then it's going to be, my age is age.

So what's going to happen? Pause the video while you have a think.

So think about variable initialization and variable assignment.

So when things are reassigned and the fact that variables can only hold one value at a time.

So that will help influence your answer.

So if you want to change it, now you can change it.

Let's look.

So the answer is, my age is 23.

And what's happened there is a variable can only hold one value at a time.

And the last value held in age before the print statement was 23.

So that's why it's just going to do my age is 23.

So take a look at these three lines of code.

What do you think each line of code will do when executed? And what would be the output of line three? So it's a new bit of code that you can see there, but there might be a bit of clue in the key word that's been used in that new bit of code.

Just, just make a prediction, you know.

It doesn't matter if you are wrong or right.

Pause the video while you have a think about it.

Let's take a look then.

So we're going to walk through this programme so that we can really see exactly what's happened.

So, first of all, you've got that print.

So print, what is your name? And it's going to be displaying as output: What is your name? for the user.

The next one is initialising a variable called name and we're assigning that to the value we're assigning to that variable is based on user input.

So that input function there that is being used is actually going to give us a keyboard prompt and allows the user to be able to type something in.

And whatever they type in will then be assigned to the variable name.

So it's got, what is your name? And then it would be blank.

And then if somebody typed in Rebecca, then their answer Rebecca would be held in the variable name as string.

It's important to note that input always returns a string value.

And we'll talk more about that later.

So then it will say back to it, hello name.

So it will display that as output because whatever you typed in on that keyboard, in this instance it was Rebecca, is now held in the value name.

So when we use that F string there and we do hello name it's going to display whatever the user typed in which was Rebecca.

So how could you use inputs to to improve your silly story programme? Pause the video while you have a little think about that.

So you can add inputs to your silly story programme to improve it so that the user can actually start entering their own adjectives, nouns and adverbs to make it even more of a silly story.

So let us do a little look in Python and see how we could do that.

So here is my silly story programme that I made.

Now, if you've lost your silly story from your lesson three then you could use the same NCC short link and get this yourself and just adapt it for this activity, if you want to.

So you could get started with that if, if you need to.

So here's my silly story.

I've got girl named Jessica, tourist, location: beach, animal, animal two, colour, clothing, and entertainment group.

So all those different variables.

And then that appears all those variables appear within the silly story.

So if I run the programme, it will give me a silly story: "Jessica went to the beach with a frog because she wanted to drive a llama.

The llama was on the beach because it ran away from his farmer to join the circus where the llama met" And so on, and so on.

So what I can do is I can actually adapt this programme now, so that it uses input.

So I'm going to do that now.

So wherever it's got a variable initialization, so this is all happening at the top, what I can do is I can have a print statement that says enter a girl's name.

Like that.

And then instead of having girl name there I can just put my input like that.

And I could just test this first one.

So if I run it, let's just run it.

There we go.

So now it's saying, enter a girl's name and this is what keyboard prompts look like in Python.

So you'll just get a cursor that sits there like that.

It's waiting for input.

So enter a girl's name.

So if I enter my name, Rebecca, and press the enter key then the name Rebecca will then be used throughout the story, depending on, so whatever you type in there in that keyboard interrupts it, asking for it's going to be placed in that variable girl name.

And then it's going to be used in the story wherever it's referenced.

So that's how it works for that first one.

I'll just do one more as a demonstration.

So again, I need to have some instructions for the user.

So enter a tourist location.

Like that.

And then instead of having tourist location equals beach, I just put in that input function call, which is there.

And I'll just do one more.

So for animal, what am I going to need to do? I'm going to have to press the enter key.

And then on this line, I'm going to have to type what? I'm going to do, print enter an animal.

Like that.

And then to the animal variable instead of having llama, I now put what I put what? I put an input function call.

And I'll just try it again now.

I'll just test it.

So enter a girl's name, Rebecca.

Enter a tourist location, London.

Enter an animal, zebra.

And then that is used in the story.

So I can carry that on for all the variables that I've used there.

So you can open up your own silly story from lesson three or you can use that NCC short link in order to find this story and change it yourself.

It's up to you, which way round you do it.

Pause the video while you improve your silly stories by using the input function.

And I've got a little code reminder there on this screen if you need it.

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

Brilliant.

We're now going to start looking at data types.

And this is something that I've mentioned here and there a little bit throughout this unit so far.

Most programming languages will require a variable to be declared before it is used.

Declaring a variable means to state what type of data will be held by that variable.

And as you know, already, Python does not require this, but it does work a little bit differently.

Python doesn't require you to declare a variable.

However, you still need to be aware of datatypes because incorrect data types can cause errors in your programmes.

There are five main data types that you need to be aware of.

So you've got string, which is just any text and that includes numbers as well but they'll always be treated as text.

So that's string.

You've got an integer, which is any whole number.

You've got Boolean, which is a value that could be true or false.

Real or floating point numbers, which would be decimal numbers.

Char, which is a single string character.

Incorrect data types can cause problems during the execution of your programmes.

So here's a question for you.

Keeping that in mind, I want you to predict what might happen when this code is executed.

So pause the video while you read that code carefully and come up with your prediction.

Let's take a look then.

So the data type for an input is always string.

It will always return a string value.

When you add two pieces of string together, it will concatenate.

And this just means join those two string together.

Instead of adding the two numbers together to make three it has joined the corresponding strings together to make one two.

This code has produced a logic error because it hasn't executed as expected.

So it won't throw up a syntax error in this situation.

It will still run, but it won't do what's expected.

So if you look there you've got the input function always returns a string.

And if we wanted to add those two numbers together we can't because they're seen as text.

So it concatenates them.

It joins them together to make one, too.

If you want Python to use your value as an integer, then you need to tell it that by casting the value.

You do this by placing input inside the int function.

And I like to call it so wrapping around the input function.

So whatever data type you want it to be, if you wrap it around that input function, then it will return that value.

Let's just take a look at it.

So input and int are both functions.

They are a type of subroutine that takes a value, processes it, and then returns another value back.

And let's just take a look at let's break it down so we've got input, process, and output.

So this is the input function, the user types 4 for example, and presses the enter key, 4 is passed to the function.

The code written for the function takes the input, removes the extra line space that was generated when you pressed the enter key, converts it to string and then returns the new value.

And then the new value is returned and 4 is typically held in a variable.

It's not always, but typically that's what would happen to it.

Now let's look at int.

So int the string value 4 is passed to the function again.

So it's odd that 4 has been passed to input and then returned the string.

And now we're passing 4 again to the int function.

The process is that the code written for the function, it takes a string value, 4, converts it to an integer and then returns a value returns the new integer value 4.

So now when it's being returned, that value is 4.

So a new number is returned and it's 4.

The new value is returned.

4 is typically held in a variable and can now be used for calculations, because now that initial string value has been returned as an integer value.

So it can be used in calculations.

Converting a value from one data type to another is known as casting.

Errors can still happen during execution, even when casting has been used.

What might happen if the user enters four, as in the word four when this code is executed? And again, just make a prediction.

You've not done this before.

You're not necessarily going to know the answer, but just make a prediction of what might happen.

Pause the video while you do that.

Excellent.

So what would happen then? So a runtime error occurs.

This is a type of error that causes a programme to crash during execution.

So this is what's happened.

It's got very confused because it's trying to do something with an integer.

It can't return it as an integer value because it is text.

So it's throwing up an error.

You can avoid this type of error by introducing validation checks.

Here is an example check that you can use, called try and except.

You will learn more about these later on in the course.

And you'll get to have a go at trying this code out as well in, in one of your activities.

So pretty much what it means is, is it tries to allow the user to enter a value, enter a number there.

But if they don't enter a number then instead of getting that runtime error, you, the value error is, is spotted.

And it just says, print int print.

You're going to need to enter a number and then they try again.

So it's going to try entering it.

If there's an error, and that's a value error, it's going to show this error, show the user that they need to enter actually a number.

And then hopefully that'll work that time.

It'll store it as an integer.

But don't worry too much if you're not quite sure about that, because we'll go into that in much more detail later on in the unit.

To convert value to different data types, you need to know the functions that are available to you.

Here are the most common functions that you will need to know.

And you can find these in the Python documentation.

I keep talking about the Python documentation a lot, and it's really, really important that good programmers look at the documentation for the programming language that they're using quite a lot.

So if you want to learn more about it then what you can do is you can go to that short link now.

Just pause the video and just have a read through it.

Some of those other functions that are available to you.

Because it's quite useful to get used to reading documentation.

Because it can be a little bit overwhelming at the beginning, but the more you get used to dipping in and out the more easy it will be to, to use that documentation.

So these are the obvious ones.

If you want to convert to string then str.

Integer's just int.

And then the final one, which is real, your decimal points, then you'd use float.

Now, what you're going to do next is create a mini data collection programme.

You'll start off by predicting, running, investigating and modifying, and then you'll have a go at your own task.

So if you're ready to go, then you can.

But if you're not quite comfortable with that, then what you can do, I'm going to go through the whole thing with you after this pause screen.

So my advice is to read that worksheet really really carefully and try your best to have a go at that independently.

But if you feel like you need an extra little bit of support then I'm going to go through the solutions next and I'm going to talk you through it.

So try it first.

If you do struggle then just come and then listen to my solutions as I go through it.

So pause the video now while you have a go.

Super.

So now I'm going to go through the solution then.

So watch the demonstration to see how you could have investigated the mini data collection programme.

Let's have a look.

Here is that programme then that you've just been looking at and investigating.

So I'm going to do that now with you.

First of all, you were asked to make a prediction and then you ran the code and possibly your prediction was correct.

It might've been incorrect.

Something might have surprised you about it, but that's absolutely fine because when you start doing the investigation you should start understanding that code even more while you look at it and you interrogate it.

So let's do the investigation together.

So step one said what data type is being collected at line two? So if we take a look at line two you've got the variable initial and then the input function is being executed there.

Now the input function always returns a string value.

So you could have put string as you answer but actually it's asking you for your first initial, which would be a single character.

So it could also be Char too.

So you might have put string you might have put Char for that.

Now the next one says, run the programme and type more than one character when asked for your first initial.

What happens? So let's have a look.

So I'm running the programme and I'm going to put my whole name instead of my first initial.

So I've just put Rebecca and an error message hasn't happened.

It's just carried on.

And it says, why do you think this? Well, I think this because we haven't wrapped around anything to make sure that just a single character is done or put any kind of statement in there so that it only looks for a single character.

So because we've just used integer inputs, all right, it's going to return a string.

So it's not going to bring up any error messages at that point.

Then it says for step four, does Python have a function for Char? Take a look at this link and read the documentation carefully.

So it was all about having a look at that point in documentation and just seeing if there was a way to have a Char wrapped around that input.

Now there is something called Char, it's got chr, you might have noticed that, but that wouldn't do what we needed it to do.

Because in this situation what we want to do is we want to return a single character.

We want to have some way of checking if a single character has been entered, and Python doesn't have a special function for that.

The CHR function is something that we'll look at much much later on in this unit.

So if you wanted it to only allow one character to be entered at that point, you would have to put in some kind of validation check that only allows one letter to be entered.

And again, we'll look at how we might do that later on.

Step five: Run the programme and type in some string or text when you are asked for your age.

What happens? So I'm going to run it.

So I'm going to put R this time, R Franks and I'm going to put some gobbledygook and it's brought up an error message.

So step six: Why did this happen? Well, it happened because if we take a look at that line of code, for the age one it's saying int input and that means that it's going to try and convert whatever that string was into an integer.

And because I didn't enter a whole number there it's not been able to do it.

So it's caused an error message.

Now step seven says: Run the programme and type in all lower case, true, when asked if you like Marmite.

What happens to the Marmite message? So let's have a look.

So I've got R Franks 22 I wish.

And then all lower case, true.

And then see what happens.

It says, well, hello, R Franks.

It is false that you like Marmite.

This is probably because you are 2.

2 decades old.

So if I put lowercase true, I feel like I've put true but actually it hasn't registered that as true.

It thinks that I've put false.

So the next step, step eight, says to put a capital True.

So what happens now? Well, hello, R Franks.

It is true that you like Marmite.

So this time it's actually worked because I've put a capital T for true.

So let's try the next one.

It says: Add hello this time instead of any kind of true.

So now it's saying, It is false that you like Marmite.

So I haven't even said that I don't like it or I do like it.

But it's saying that I don't like it at this stage.

And then it says: Line 12 contains the following piece of code, likes Marmite is equal to Marmite.

So if you look down here, I've got line 12 likes Marmite is equal to Marmite and it's there in the curly bracket.

So it's going to do something there with those variables, likes Marmite and Marmite.

What do you think might be happening here? Now, this is something called a Boolean statement.

And what we're saying there if we look at the variable likes Marmite.

So if we go up here, we've got line eight, likes Marmite and then it's accepting input from the keyboard there.

And when I typed in a capital T for true that was the only time that it said true on the screen.

So if we look at the next one, it says Marmite equals true.

Now notice that that matches that true with the capital T that I typed in.

And that was the only time that it worked out as true.

So if we look back now at this Boolean statement there, we've got likes Marmite, so whatever I typed in here will be held there, is equal to Marmite, which is true.

So what we're saying here is likes Marmite.

So whatever I've typed in there is equal to whatever is being held in Marmite.

So if I've got a true and a true both with capitals then that condition is going to evaluate as true.

And that's why it was saying true.

But if I've got for likes Marmite, if I've got a lower case true or false or hello or anything other than that capital letter True, then that word is not equal to True with a T.

So it's evaluating it as false.

And that's why it would say it was false.

And this is something we'll we'll look at in much, much more detail later on when we start looking at conditions.

So for step 11, it says Run the programme and type 9, when you are asked for your age.

What happens? So let's try it again.

So run the programme and type 9 when you are asked for your age, what happens? Okay.

So first initial Franks type 9, true or false you like Marmite.

I'm going to put True.

Well, hello, R Franks.

It is true that you like Marmite.

This is probably because you are 0.

9 decades old.

Okay.

So it says on there 0.

9 decades old.

And it says on line 10, the forward slash is being used between the variable age and the number 10.

So here we go.

So here between the age, there's a forward slash between there.

What arithmetic operation is forward slash performing? So what do you think is happening there? If you've used a spreadsheet before then you probably know what that's doing.

And it's actually dividing.

So let's look at the next step then.

It says on line 10, change the word float to int.

Run the code and type 9 as your age to see what happens.

So, got to change this float to int, I've got to run the programme.

So it's now not a decimal number.

It's a whole number.

So I wonder what's going to happen.

Oh.

I did it wrong.

Try it again.

Have to put a 9! Silly me.

Right, R Franks 9 True.

Okay.

So now it's saying this is probably because you are zero decades old.

Okay.

So we've done the on line 10.

So we're on step 14, keeping line 10 as is, run the code and type in 28 as your age.

What happens? Lets try it again.

So R Franks, oops, 28, True.

And this time it's saying you are two decades old.

And then it says what do you think is happening when the number is held as an integer compared to a float? So what it's doing is it's not holding it as a decimal number this time.

It's holding it as a whole number.

So when it's doing it as a whole number, it's not going to have anything after that decimal point.

So that, when it was, should have been 0.

9 and it was 0, anything after that decimal point isn't held in that value.

So if we want a decimal point, we've got to make sure that we use float.

And if we're happy with the whole number and we want a whole number, then we need to use int.

And now finally should have done all of our investigation there.

And by doing that investigation, you should have hopefully really started to understand what this programme is actually doing and what some of those data types are and how they're actually working.

And then finally, you were asked to modify the programme just by having a go at that try and except.

So I've got one here and I've got my bit of code so I want to do it on the number one.

So with the age.

So if I wanted to put for the age, try and except, what I can do here is I can put in try like that.

And then if I press backspace and then just press the enter key, it'll put that indent exactly where I want it.

So it's going to try allowing that to type in, and then we can have except ValueError like so, except value error.

And then I can have print.

You must enter a number, like so.

And then have that same thing again.

So age equals int input like that.

So now if I type in a bit of text into age it shouldn't bring up that error message anymore.

So let's just go and have a look and see what happens.

So if I put gobbledygook now, it says you must enter a number.

So then you got to put 22, it carries on.

So that error message didn't happen.

So those sorts of validation checks in there are really really handy.

And you'll learn much more about them later on.

Because if you may have noticed, let's just do a little bit of an extension here while I keep talking.

Let's just run this again.

So you may have noticed, if I actually type text in twice.

You must enter a number.

If I put text in again, I'm going to get that error message.

Now this is something you'll learn about how to fix much later on in this unit.

Let's see then how much you can remember about data types.

You should have picked up quite a lot there about what they are and what they look like.

So what I'd like you to do is pause the video.

You can draw that out exactly as it is using your pen and paper.

And I want you to match the data type with the example value that the example data there.

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

Brilliant.

Let's have a go and see and look at the answers then.

So Boolean, the answer was False.

So Boolean can only hold the values true or false.

And we had to look at that there with that Marmites line of code.

String was "Hello." So it was just, it was a full word there and it's in those speech marks, so we know it's string.

Now, Char still looks like string but it's just one single character on it's own.

Real/float, it's a decimal number.

And integer with just a whole number.

So no decimals there.

And that's the end of the lesson.

So hopefully you started to understand there a little bit more about data types and data validation, use that try and except.

And hopefully as well you now know how to use input and the different ways that you can wrap around int and float to those input statements so that you can get different data types.

Now, if you'd like to please ask your parent or carer to show your work on Instagram, Facebook, or Twitter tagging @OakNational or #LearnwithOak.

Because you might want to share what you've done this lesson on those media.

That'd be great.

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