Lesson video

In progress...

Loading...

Hello, my name is Mr. Hogan.

I'm excited to be learning with you today.

We are going to have such a great time learning about programming iteration.

I will be supporting you with our learning during these lessons.

I'm so pleased that you have decided to learn about validating data in a programme.

We are going to do brilliantly.

Fantastic.

Let's have a look at the outcome of the lesson.

So the outcome is, "I can explain the purpose of validation checks and use iteration to ensure data entered is reasonable." So I'm really looking forward to this lesson.

It's all about making sure that any data entered into your programmes is reasonable.

We have two keywords in today's lesson.

Validation.

So validation is checking that data is acceptable by comparing it against a set of criteria.

We also have crash.

So this is when a programme suddenly stops working and closes unexpectedly, often due to an error it cannot recover from.

So these two words are going to be used throughout the lesson, and you can always come back to this slide to remind you of what they mean.

So the lesson is split into two.

We have the first part, which is, "Describe why validation is used in programmes," and the second part is, "Use iteration to repeat until data is valid." I'm really looking forward to this lesson.

It's going to be super useful for you when you create your programmes.

So let's move on to the first part of the lesson, which is, "Describe why validation is used in programmes." Laura is asking, "What will happen if a user enters an unexpected value whilst using my programmes?" Good question.

Another one.

"How might this affect the robustness of my programmes?" So, Laura's really good asking these questions.

So programmes need to cater for all likely inputs.

You need to predict what could be entered by the user and ensure that checks are in place to handle unexpected data entry.

A user is prompted to enter number between one and 10.

People may enter lots of different possible responses such as these.

So, it's the five, as in the word five spelled out in characters.

It could enter 7, 7.

5, 12, -5, maybe totally empty, maybe an empty string where the enter key is pressed, maybe like even an 8 followed by a square bracket.

But there are four main categories for these data entries.

So correct is like number 7, out of range, which can be out of the range so like 12 or -5, what we call a ValueError, which is the five typed out in characters, 7.

5 or 8 followed by the square bracket, and an empty screen.

That's the last category.

So that's where the enter key is pressed with no input.

So let's have a quick check about this.

What causes an empty string error? Is it A, a number being entered, B, space bar being pressed, or C, enter being pressed? Remember, you can pause the video at any time or rewind it.

So let's have a look at the answer.

So the answer is C, enter being pressed.

Yeah, that causes the empty string error.

So nothing has been entered.

Data validation is the process of checking whether input data is acceptable before the programme uses it.

For example, if a user is asked to enter their age, the programme should check whether the input is a number and within a realistic range.

A crash happens when a programme suddenly stops running because of an error it cannot process or recover from.

If a programme prompts the user for a number, and the user types dog, the programme doesn't know what to do, so it crashes.

You may have come across this in your own programmes where the programme just stops suddenly, and that's called a crash.

So using validation ensures data is reasonable and helps prevent crashes in the programme.

It also helps maintain data accuracy and reliability, and improves the user experience by catching mistakes early.

Without validation, someone might enter dog as their age, and the programme would crash.

So let's have a quick check.

Which of the following is a benefit of using data validation in a programme? Is its A, it ensures data is reasonable and helps prevent programme crashes? Is it B, it makes the programme more difficult to use? Or is it C, it reduces data accuracy and reliability? Remember, you can pause the programme at any time or rewind it.

So let's take a look at the answer.

It is A, it ensures data is reasonable and helps prevent programme crashes.

Yeah, great use of data validation.

Validation is usually used inside a loop that keeps asking for input until it is valid.

Often, validation uses "if" statements to test the input.

So we're gonna take a look at this code so we can see that on line three, we're asking if age.

is digit.

Let's take a bit of a closer look.

So on line one, we've got "while True." So this creates an infinitely.

It keeps running until the programme stops by using a "break" on line four.

The "break" statement immediately stops the loop.

In this example, "break" ends the "while true" loop once valid input is given.

Without it, the loop would run forever.

"Break" only stops the loop it is inside though.

"If age.

is digit" checks whether the user has entered only digits, zero to nine.

If they've typed something like 12, this will be true.

If they've typed something like twelve, spelled out in characters, or left it blank, this will become false.

Let's have a practise question now.

So one, "Describe the purpose of validation in a programme." Remember, you can pause the video any time or rewind it.

You may take some time to do this and answer the question.

So a possible answer.

"Validation is used in programmes to check that data entered by the user meets certain rules before the programme continues.

This helps prevent errors or crashes.

For example, a programme might check that the user enters a number within a specific range or avoids leaving a blank field.

Without validation, the programme could break or produce incorrect results." So well done if you've got an answer similar to this, so you may have picked up on two, three, four points from this answer.

So take your time to read it and you may want to update the answer that you've done.

Brilliant.

Well done on getting this far in the lesson.

That's the first part over.

Fantastic.

And I'm so excited to be moving on to the next part, which is, "Use iteration to repeat until data is valid." This programme uses a "while True" loop to check if the input is not an empty string.

If it is empty, so the user just presses enter, it reminds them and tries again.

Once a name is entered, it breaks the loop and prints the name.

So take your time and look at this code.

Sometimes, programmes crash when something unexpected happens.

For example, when a user types a word instead of a number.

To stop this from happening, you can use "try" and "accept." Try, this is where you put the code that might cause an error, and accept, is if an error happens in the "try" block, Python jumps to the "except" block to handle it without crashing.

Okay, so "try" is where you put the code that might cause the error, and then "accept" is like an exception.

If an error happens in the "try" block, Python will jump to the "except" block and execute the code there without actually crashing.

So let's have a quick check.

This is a true or false one.

"Try" and "accept" blocks are used to prevent a programme from crashing when an unexpected error occurs.

Is this true or false? Remember, you can pause the video at any time.

So the answer is true.

"Try" and "accept" blocks are used to prevent a programme from crashing when unexpected errors occur.

As well as using "try" and "accept" to stop the programme crashing, you can catch specific types of error like ValueError.

A ValueError happens when you try to convert a string into a number, but the string is not a valid number.

So take a look at this code.

This gives more control and avoid catching unrelated errors by mistake.

This programme asks the user to enter a number between one and 10.

It keeps repeating the question until the user gives a valid number.

So this is where we've got our "try" and "accept." If the user types something that isn't a number or a number outside the allowed range, the programme gives a helpful message and asks again.

The programme begins a "try" block, where we try to run some code that might cause an error.

If an error occurs, Python will jump to the "except" block.

If the user enters something that can't be turned into an integer, like a word or symbol, the Python jumps here and displays an error message, on line eight and nine.

It keeps repeating the question until the user gives a valid answer.

Let's have a check.

What is the purpose of using a "while" loop for data validation? Is it A, to make the programme run faster, B, to repeat the input process until the user enters correct data, or C, to display the data in a different format? Remember, you can pause the video anytime or rewind it.

The answer is B, to repeat the input process until the user enters correct data.

Yeah, that's the purpose of using a "while" loop for data validation.

Well done on getting this far.

I hope you enjoyed the lesson.

I sure am.

So now, time for a quick practise.

One, write a Python programme that asks the user to enter their age to join a junior sports team.

A, To qualify, the age must be a whole number between six and 13, inclusive.

B, If the input is not a whole number or outside this range, the programme should display an error message and keep asking until valid input is given.

C, Once the age is valid, print, "You are eligible to join the junior team at age X." Replace X with the actual number.

So take your time.

It'll mean you're going probably pause the video and coding this yourself on your own IDE.

You can always rewind the video to see what we've done previously.

I think that will help you a lot.

So we have a possible answer here.

So take your time and have a look.

So we've got the world "truly" that starts on line one.

We've got our "try" on line two.

So we're saying "Enter your age" on line three.

And then on line four, we've got an "if," so if it's equals to or greater than, and age is less than or equals to, so that makes it that inclusive part, we can break and just print.

Go to line 11 and print that "You are eligible to join a junior team at age" of whatever values held in age.

Else, on line six, we print, "You must be between age 6 and 13 to join." And then we've got the "accept" there.

So if the user enters anything else apart from a digit, it goes to a ValueError and we print, "Please enter a whole number." And that part we will repeat.

So well done if you've got something very similar to this and you've got it working.

Brilliant.

Well done.

You've come to the end of the lesson.

So in summary, you can explain the purpose of validation checks and use iteration to ensure data entered is reasonable.

Iteration can be used to repeatedly check the user input until reasonable and valid data is entered preventing the programme from continuing with incorrect values.

You've done brilliantly to get this far.

I really hope you enjoyed the lesson.

I sure I did.

And hopefully your programmes will become more robust.

Well done.