Loading...
Hi, I'm Rebecca, your computing teacher for the programming part three unit.
For this lesson, you're going to need your Replit account, which you should have already set up with your parent or carer's 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 also to make notes if you need to.
It's a really good idea to try your best to remove as many distractions as possible as well, so that you can really focus in this lesson.
Once you've got all of that ready, we can begin.
In this lesson, you will determine the need for validation checks, and you'll use iteration to perform validation checks.
Make a prediction about this programme then.
So during the execution of this code, if a user enters the string, one at the first attempt and then one at the second attempt, what will happen? Pause the video while you have a think about that.
Let's take a look then.
So there will be a value error because the string value one cannot be converted to an integer by the end function.
The user is only reminded once to enter the data correctly.
The second one causes the error.
So what will happen when a user enters an unexpected value whilst using our programmes? Pause the video while you have a think about that.
It can cause runtime error that will stop the programme from working.
How might this affect the robustness of our programmes? And I've got a little definition there.
So, robust just means strong or unlikely to break in this situation.
So pause the video while you have a think about that.
If a runtime error can easily occur, then our programmes will not be robust.
Our programmes need to cater for all likely inputs.
We need to predict what could be entered by the user and ensure that checks are in place to handle unexpected data entry.
So, here's a question for you.
A user is prompted to enter a number between one and 10.
What are the likely inputs for this scenario? And I want you to pause the video while you write down all of the likely inputs that you can think of.
Let's take a look then.
So here are some of them that I have listed, so they could write five as in the word five, and then the number seven, 7.
5, 12 minus five, they could put an empty string, so they could just press the enter key, and they could press an eight but I accidentally put a square bracket there because that's quite close to the enter key.
So there's all sorts of likely inputs there that could happen.
There are four main categories for these data entries.
So, they're either correct, they're out of range, value error, which is one we've looked at already, and an empty string or they're pressing the enter key.
What I'd like you to do is complete the all likely input section of your worksheet to place the likely inputs into those four categories.
Pause the video while you have a go at that.
Let's take a look at the answers then.
So, here are the answers.
So you can pause the video while you mark your work.
Excellent.
Now, here is a validation check to make sure that an integer is entered as input.
At the moment, he only works once.
Let's take a look at how we can introduce a while loop to ensure an integer is entered.
So let's take a look in Replit.
Here is that programme then in our Replit account.
So, I'm going to run it to see what it actually does, and this is the same one that we use at the beginning of the lesson.
So you should all be quite familiar with what it does.
So if I time pin one in text, it'll say you must enter a number.
But if I type in one in text again, I get this value error at the bottom because I've typed in string again.
And there's no check here to see whether we've got to try and accept again at this point.
So that's why it's not working as we'd like it to, because we want in our programme, for it to always handle those errors and always prompt us to put the right thing in so that our programmes don't break during run time.
And that's what's really important here.
So I'm going to introduce some repetition.
Now, I could use a while loop or a for loop.
Which is the best loop for me to use? Do you think a for loop would be a good choice for this situation? Do we know how many times they're going to get it wrong? We don't know how many times, do we? So if we don't know that sequence already, then we want to be using a while loop because a while loop will keep running while the condition is true.
Let's just remind ourselves of how a while loop actually works.
So we've got while true.
While this is true, do something, okay? So that's our basic structure for our while loop.
So while something is true, keep doing something.
So, we've got to think, well, what needs to be true in order for this block of code to run? And what we're doing is we're checking if the data entered is valid.
So what we could do is we could set up something called a Boolean variable, and that's where the value could either be true or false.
So I'm going to call my Boolean variable, not validated, because at the beginning, their input hasn't been validated because it's the very first time, I haven't even looked at it, that bit of code hasn't even been executed.
So I can say not validated is true at that point.
Then I can just start my while loop.
So while not validated.
So while that number that's going to be entered isn't validated, keep trying to validate it.
That's basically what I'm saying here.
Now, I've introduced this while loop, so I've got to make sure that this block of statements here are within that while loop block.
Now, a quick way of doing that is to highlight all of the text, and press the tab key, and then it moves it all at the same time.
So while not validated, try this.
So print the number, have a go at it.
I'm going to get rid of that.
And see if you can make a prediction about what might happen before I run this programme.
So you may want to pause the video while you make a prediction.
Let's take a look, then I'm going to run it.
So it says enter number, and I'm going to get it wrong.
You must enter a number between one and 10, and I'm still getting that value error.
So, did you predict that? Why do you think that's happening? Let's take a look at the code.
So first of all, if we look at that first line of code, it's done enter a number between one and 10.
That's up to you, fine, it's allowed me to type it in.
Then it's reminded me I must enter a number between one and 10.
So it's gone to this line of code because there was a value error.
Then it's gone to this line of code again.
So it's asking for that integer input again.
And you might have thought, well, it's going to work this time because it's going to go up here, and it's going to do, try and accept again.
That's not how it works in this situation, the try and accept happens here.
So we would need another try and accept here.
But because we're in a loop, actually, we don't even need this line of code at all.
And you're going to hashtag it out, just so I can prove it to you.
So I'm going to run it.
So enter a number between one and 10, I'm going to put F.
You must enter a number between one and 10, enter a number between one and 10, I'm going to put F.
You must enter a number between one and 10.
So now it is working.
So I know I do not need that second one, but let's just run it completely without that.
And I'm going to keep getting it wrong, wrong, wrong, wrong, wrong, wrong.
I'm going to get it right though.
And it's saying, enter a number between one and 10.
Still getting it right, and still getting it right.
And still getting it right.
And I am stuck in a loop now.
So why do you think I'm stuck in a loop? Pause the video while you have a little think.
So let's just take a look at this programme, let's just trace it through.
So, we started off with that Boolean variable not validated equals true.
And then here, we haven't changed that value at all between line one and line three, so this equals true, the first time this is executed.
So because it's been initialised as true and then this is the first one, we actually reference it, it's still true.
Then it does these lines of code.
And there is nothing in this block of code that changes the value of that variable to false.
And because of that, it's just going to keep running and running and running and running.
So what we need is we need another statement that says, instead of not validated equals true, we need not validated equals false.
And that needs to be somewhere in this block of code.
So just pause the video while you have a think about where that might need to be.
So a tricky one this is, 'cause you probably don't know enough about try and accept to be able to figure this out.
So let's just look at the programme again.
So we've got try.
So print, enter a number, and get them to enter a number.
Now, if at this point a value error occurs, then it's going to go to this line of code, and it's going to print, it must be a number between one and 10.
But if there's not a value error at this line of code, then it'll just go to the next line of code.
So if there was a line of code here, it would just move along to that line of code.
In fact, I'll just put validated there so that you can see.
So, this line of code actually needs to go, what am I doing? So this line of code needs to go here after the input.
So then if they enter it correctly, not validated will now be false because it has been validated, so it's the opposite.
And then it should run that line of code.
So let's just try it now.
So I got it wrong, wrong, wrong, wrong, wrong, wrong.
And then, right.
And it says validated.
So the loop terminates, and then it goes to the next line of code in the main programme.
Now you've watched me do it, have a go yourself.
So there's the link to that starter code that I use.
See if you can add those validation checks in yourself using that while loop.
Pause the video while you have a go.
Brilliant.
Now, it's time to have a look at the next activity.
So use the adding validation check section of the worksheet to add validation checks to your programme.
Pause the video while you have a go at that.
Excellent.
So, let's take a look at how you could have completed the last task.
What I'm going to do then now, so I'm going to work through how I would have answered those questions on the worksheet.
And I've got the worksheet here in front of me, if you're wondering why I'm looking down and reading those questions.
So I'm going to go through it.
And this can either check whether you're correct, or support you if you struggled with some of those questions.
So let's see how I would do it then.
So it says step one.
And if statement is required to check if the number entered is within the correct range.
So incomplete code has been created below to assist you with this.
Thank you, that's very helpful.
And it says tick as you go along.
So we've got complete the code and incorporate it within the while loop.
It says, test your code to make sure that it works as expected, and use the table on the next slide to help.
So, again, these input output tables that are on your worksheets are so, so helpful because they allow you to see exactly what should be output on the screen.
And then that allows you to think how the code behind it might actually be working.
So it's really, really useful to check those tables, especially if you are struggling a little bit with what exactly you've got to do.
So, if I look at how the programme is meant to output, it's meant to say, enter a number between one and 10.
And then if I do something incorrect, like enter an F, it says, you must enter a number between one and 10.
And then enter a number between one and 10, and then I'll put 12 in, and then it says number entered out of range.
So, we've got to make sure that that number is validated, first of all.
So they've got to enter a number, but then we've got to check if that number is correct or not.
So, what I want to do is I'm going to make sure that that is also in the while loop too.
So I'm going to put it here because I don't want to say not validated equals false, and then start entering, checking if it's between one and 10 because it's not been validated at that point because we don't want to just make sure it's a number, we want to make sure it's a number between one and 10.
So this is not going to be false until I've checked that.
So I'm going to put in, so if number is greater than zero and number less than 11, then do that.
So it says else, print, enter, print number entered out of range.
So, else print number entered out of range.
Let's just see what that does, let's just run it and see if it works.
So enter a number between one and 10.
I'm going to follow the input output table.
So I'm going to put an F first.
You must enter a number between one and 10, and then next it says 12, so I want to put 12, number entered out of range.
So it's working, enter a number between one and 10.
So now I'm going to enter a four, and it's moved up.
Well, it's terminated the loop and it's actually terminated the programme 'cause it's got to the end of the programme.
So I know now that that little bit of code is working, so I can move on to the next one.
Now, it is saying to check for all likely inputs, because if you look there, I didn't check every single situation, I just checked the ones that were on that table.
So, I need to check if the correct data is entered.
So between one and 10, so I can do my first check.
So if I enter a number between one and 10, does it just let me do it? And yes it does.
So I know that that one works.
Then it says the data entered is out of range.
So if I do something out of range, and I know I've already done this, but I mean, I could put minus two and see what happens.
And it says numbered entered out of range.
So I know that that definitely works.
And then the data entered is not an integer, so I can put 5.
4.
And again, it said, you must enter them between one and 10.
Really, it probably should say a whole number to be a bit more specific, but that's okay at the moment.
And it says no data is entered, so the user presses enter without putting any input in.
So let's just try pressing enter.
And again, it's working.
So we know now it's most likely working.
So it's a good idea as well, to put something in wrong couple of times and then put numbers, and then put the correct number in too, just to make sure.
So, that is how we check for if a number is entered and it's within a specified range.
Now, the next task was to do an enter a name programme.
So something a little bit different.
It says open up a new programme.
So I'm going to go to new and I'm going to put enter name as my programme.
Then it says, create a new file and enter the code below.
So I'm going to put the code in, so print, enter a name, and then I've got name equals input.
And I've got print and I've got an F string stored name, {name} like so.
So, that is my start code.
And then it says, create a wallet that will continue to ask for the name, if the user presses the enter key.
And it gives you a little tip as well.
It says, if nothing is entered in name, then it will equal to just two speech marks because that will mean blank.
So I've got to introduce a while loop.
So I know I've got to do that, so I'm going to put that at the top.
But I've also got to think, well, what needs to be true in order for this while loop to run? And I'm just going to do the same thing that I did last time, I'm just going to have that not validated.
And which means I've got to set up my Boolean variable at the beginning.
So not validated equals true, just so that it runs that first time.
Then I've got to think, well, what code needs to be in there? I've got a feeling this final line of code should be outside the loop, so I'm just going to do it outside the loop for now, I might be wrong.
I'll have to change it later, but that's okay.
So now I've got, well, not validated, enter a name, and if I just run this, if I just test it just to see what's going on with it.
So if I enter a name, it is going to keep asking the same question over and over again.
And if I put it blank, it's still asking the same question over again.
So the moment is not checking if I've left it blank.
So now I've got to put in an if statement to see if it has been left blank.
So I'm going to have, if name is equal to just two speech marks like that, print.
You must enter a name, like so.
So now I'm reading it again and see what happens.
If I leave it blank, you must enter a name, you must enter a name.
If I type in a name, it's still saying, enter a name, but it's not saying you must enter a name.
So it's not running this line of code.
And that's because what I'm typing isn't equal to blank.
So it's still stuck in a loop, even if I get it right.
So now I've got to think, well, how do I change not validated to false? So I'm going to have to have a line of code that says not validated equals false, but I've got to think, well, where does that need to be? Now, you might think, well, I could just put it here.
So not validated equals false.
But let's just see what that actually does.
So if I type in, if I just put in enter key, the loop breaks.
It says, you must enter a name, stored name, and then the loop terminates.
And that's because we've said whether they're right or wrong, not validated equals false.
So that's not going to work correctly.
What we actually need is an else 'cause if they get it wrong, we want them to enter a name again.
But if they don't, we will not validate it to be full.
So I can just do else not validated equals false.
And let's just see what happens this time.
So I'm going to get it wrong.
And then I'm going to put in a name, and now it says, stood named Becky.
The loop is terminated, and it's now executing line 12, which is excellent.
Task four then was to take the times table quiz that you made previously, and add in a while loop for those validation checks as well.
And if you didn't have access to that times table quiz, you could have opened up the one that's here on the link.
And this is what you're presented with if you do go to the one that's on the short link.
Now, if I just run this programme, you probably would have spent a little bit of time just reading it through just to remind yourself how it worked.
And there may be running it.
So if I run it now, it says, enter a times table that you would like to be tested on.
If I type in loads of letters and I press the enter key it says you must enter a whole number.
If I put loads of letters in again, I get this value error because it only does the validation check on that first time.
And on the second time it doesn't.
So this programme would break in that situation.
No, I just run it again, let's just stop it and clear it.
So I've got here quite a few opportunities for input.
So we've got the times table, and it does say this on the worksheet, I think as well.
We've got the times table, we've got max value.
I'm sure there's another one.
Maybe it is just two, I'm sure there was three.
Let me just have a look.
Make a list of the three variables that are assigned an input.
So there must be another one there somewhere.
What times table max value.
Ah, use the answer, there's the other one, I spot it now.
It's right down here.
Gosh, that was quite hard to find.
So, you've also got user answers.
There's three opportunities for user input there.
And then you were asked to think about the likely inputs that could have been put in those situations.
So things like text or something that might be out of range possibly.
So there were some likely inputs there.
And then you had to use what you'd learned in those previous tasks to put in a while loop wherever those validation checks are so that it did some validation checks.
So I'm just going to do one for you just to show you.
Now, it can get quite confusing when you start getting these big long programmes and you have to start adding more code into them.
And when we start to properly use subroutines in the next part of this unit, you'll start to see how you can clean up your code a little bit better.
But at this point while we're just practising , we're just going to do this way.
So I'm going to go here.
We've got try, except, try, except.
We haven't even got a try, except on this one.
So I could add one in there too, but I'm just going to put a bit of outer while loop here.
So I'm going to put while not validated.
Yeah, while not validated, like so.
I also need to make sure I've got not validated equals true there.
And then I've got to tab in all of this, and then I've got to think, well, what did I do last time? Last time, I've got rid of this because I didn't need that anymore.
And I also added in the not validated equals false.
So that has now, with this first user input where they put in which times table they want to do it for, it should not let me carry on until I've entered a number.
Okay, so now it is working, and now it's going to this next bit.
So enter the maximum value for your times table, which is line 10.
So it has come out of that loop and it is now moving on to the next part in our main programme.
So, I could do the same thing.
I could put in some more validation there, where for example, I could have, if, where's the variable? If times table less than one.
So if you see you bro, then print your number must be higher than zero.
I could put in a validation check like that, and then I just need to have my else.
So then it goes to the not validated.
So that's another thing that I could do.
So I could just try that again.
So I could type in minus four, your number must be higher than zero.
You must enter a whole number, and now I've entered a whole number.
Now, you might want to put another range in there.
So you might want to put and less than 100 possibly 'cause you might not want to put a huge number in there.
But I mean, it doesn't say that in instructions at the top, at the moment people could put any number in there.
So you might want to limit that too.
So, you could just do exactly the same structure then again for this one down here.
So max value as well could have the same validation check to see, we've got value error and put that in a while loop.
And then the same here for the answer just to check it to whole number.
So, that would hopefully just give you a bit of an idea of what you might've needed to do.
And if you want to extend that further as well, you can do.
Let's finish up then with a Parson's puzzle.
Use the Parson's puzzle section of your worksheet to solve the puzzle.
Excellent, let's look at the solution then.
So here is the solution.
You can mark your work by using that.
So pause the video while you have a go at marking.
Excellent.
So now, hopefully you've got a better understanding of why we need data validation checks, and how to implement them in your programmes, especially there with using a while loop so that it really doesn't let any of those errors occur.
And if you'd like to, please ask your parent or carer to share your work on Instagram, Facebook or Twitter, tagging @OakNational and #LearnwithOak.
And I'll see you again soon for our final lesson in this unit, lesson five.