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 all about programming iteration.

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

I'm so pleased that you've decided to learn about designing robust programmes.

We are going to do brilliantly.

Fantastic.

This is gonna be a great lesson.

I'm really looking forward to it.

So the outcome of today's lesson is that I can explain why robust programming is important and use exception handling in Python to catch errors.

We've got two keywords this lesson.

We've got robust.

So this is a programme that handles errors or unexpected input without crashing.

And we've got exception handling, so code that catches and manages errors so the programme doesn't crash.

So we'll be using these terms throughout the lesson.

And remember, you can always rewind the video and return to this keywords slide to remind you what these words mean.

Great.

Let's move on to the lesson.

This lesson is split into two.

The first part is explain why robust code is important, and the second part is apply exception handling in Python, so actually using exception handling in our code.

I can't wait for it, so let's move on to the first part.

Explain why robust code is important.

Sam is saying, "My programme keeps crashing when I run it.

I just typed in dog instead of a number, and everything broke." Alex is replying, "You need code that can handle unexpected inputs without crashing." Alex is quite right.

So robust programmes are designed to handle a variety of unexpected situations or errors without crashing.

So we want all our programmes to be robust and not crash.

Sam is adding, "That's exactly why we need robust code.

It will make my programme more reliable and easier to use." And Alex replies, "Exactly! Whether someone presses enter by mistake or types the wrong thing, robust programming helps your code continue running." Alex is right again.

So robust code helps your programme keep running, even if something unexpected happens.

It handles unexpected situations or errors without crashing.

And robust code also includes checks and safeguards to make sure data is valid and errors are handled properly.

Let's have a quick check.

What is the main characteristic of a robust programme? Is it A, it runs very quickly? B, it can handle unexpected situations or errors without crashing? Or is it C, it always produces the correct output no matter the input.

So you can pause this video at any time or rewind it to help you.

So let's have a look at the answer.

It is B.

So the main characteristic of a robust programme is it can handle unexpected situations or errors without crashing.

Well done.

So there are three types of validation checks that can help make your code more robust.

So an empty string check ensures the user has entered something.

Minimum length check validates that input meets a minimum character requirement.

And then range check confirms that numerical input falls within a set range.

So those are the three types of validation check we're gonna look at, empty string, the minimum length, and the range check.

When the following code is executed, the user presses Enter without typing anything and the programme still runs, but the name is left blank.

This new code checks whether the input is empty, which is called an empty string check.

If it is, it asks the user to try again.

The while loop, which is highlighted, repeats the question until something is entered.

This makes the code more robust by preventing it from continuing with missing input.

It also improves the user experience by guiding the user to provide valid data.

So let's have a look at the code.

So on line two, we use a while loop to check if the input is empty.

In other words, if the user just pressed Enter without typing anything.

If the name is blank, we show them a message asking the user to enter something, so we do that on line three.

And then we ask for their name on line four.

And then the loop will keep repeating until the user types something in.

And then finally, once we have a valid name, we greet the user.

When the following code is executed, the user is prompted to create a password.

The user may enter a password with very few characters, so we've got there a very simple code, create your password and then line two password saved is printed and displayed on the screen.

Now, this new code checks the length of the input, which is known as a minimum length.

The code checks if the password is shorter than six characters by using the len(password) function.

So we're saying, it's highlighted here, when the len(password) is less than six, so the length of password is less than six.

If it is, the loop runs, asking the user to enter a new password.

Once the user enters a password of six characters or more, the loop ends and the password saved message is printed and displayed.

When the following code is executed, the user enters their age.

However, the user might enter an age like 150 or minus five, which is not sensible.

So you can see there some simple code on lines one and two, asking the user for an integer input for their age and then printing you are the value held at age years old.

This new code checks if the ages entered is between 13 and 100, which is known as a range check.

So you can see that highlighted on line four.

Now, the try and except block is used to catch errors if a non-number is entered.

So on line two, we are saying try and then we're catching the error on line eight by using the except value error print please enter a number on line nine.

If the input isn't a number between 13 and 100, the programme prompts the user again, instead of crashing it there on line nine.

Let's have a quick check.

True or false? The code while code equals to empty string will keep asking the user for the code until they enter something.

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

So let's have a look at the answer.

The answer is true.

Why is it true? Well, the while loop continues to execute as long as the name variable is an empty string, prompting the user for input again.

So I hope you got that right.

If you didn't, don't worry about it.

You can always go back and rewind the video and replay parts that you didn't understand.

Let's have a quick practise.

So one, explain why writing robust code is important, even if a programme works correctly with the expected input.

So take your time to write the answer to this.

Remember, you can pause or rewind the video at any time.

So a suggested answer.

Writing robust code is important.

It means creating programmes that can handle unexpected situations and errors.

Even if a programme appears to work fine with the first few things you try, real users might enter different types of data.

Robust code includes checks and ways to deal with these unexpected inputs, preventing the programme from crashing.

This makes the programme more reliable and user-friendly.

So that's the suggested answer.

Hopefully you've got some main points out of that correct.

Perhaps you want to go back to your answer and add to it or change parts of it to make it more similar to this, but well done in answering it.

Brilliant.

Okay, so we've done the first part of the lesson, which was explain why robust code is important.

Now, we're gonna move on to the second part of the lesson and we're really excited to be doing this where we're going to be applying exception handling in Python.

Sam is asking a question.

"My programme keeps crashing when someone tries to type in the wrong thing.

It says something about a ValueError." Alex is saying, "That happens when Python hits an error it doesn't know how to handle." So in this case, this is known as exception handling.

So this is used to manage errors in a programme, preventing it from crashing and making the programme more reliable.

Sam is saying, "We have seen that try and except along with ValueError can be used to catch errors." Alex is asking a question.

"Yes, I wonder if there are any more." So yes, Python's built-in exception handling can be used to make code more robust.

So Python's built-in exception handling, such as using try and except, allows you to detect errors before they crash your programme, respond to those errors in a controlled way by printing a message or asking user to try again, and also allows you to prevent the entire programme from stopping unexpectedly.

Let's have a check.

It's a true and false one.

So exception handling allows you to manage errors that occur during the execution of a programme.

Is that true or false? So take your time to think about it.

You can always rewind the video to help you.

Okay, so we have a look at the answer.

It is true.

Why is it true? Well, exception handling in Python, typically using try and except blocks, is the method used to manage runtime errors.

The following code will crash if the input is not a number.

It is expecting an integer to be entered.

Yeah, so on that line, you can see the int is for the integer to be entered.

This code uses the ValueError exception type to catch any invalid values being entered.

The try block contains code that may potentially raise an exception.

So we can see there the try and it's going to potentially raise an exception to an integer being entered.

If a ValueError occurs during the execution of the try block, control is passed to the except block of code.

You can see on line three there where it's all highlighted now.

We've got the ValueError and a prompt is then displayed.

The following code asks the user to input a number and then divides 10 by that number.

Without exception handling though, if a user enters zero, the programme will crash with a ZeroDivisionError because division by zero is not allowed in mathematics.

So take your time to look at those three lines of code.

This code uses the ZeroDivisionError exception type to catch any invalid values being entered.

So we can see there the except block of code where we're not going to allow zero to be entered.

If it does, we can prompt the user on line eight.

However, the code will crash if an integer is not entered.

So it will still crash if a character is entered.

This code uses ValueError and an additional except block of code to catch any invalid values being entered.

So we can see on line seven, we've got an except block for the ZeroDivisionError from the previous code we looked up, but now we've also put in another except block on line nine, which is the ValueError.

So now we can't put in a zero or any characters.

Let's have a quick check.

What kind of error will occur if this code is run, x equals 10 divided by zero? Have a quick think about it.

Remember, you can pause or rewind the video to any point to help you.

Shall we have a look at the answer? It is B.

Yeah, well done if you remembered it.

It's the ZeroDivisionError.

Time for a practise.

So write a programme that asks the user to enter two whole numbers.

The programme will then divide the first number by the second.

You need to handle the following errors.

A, if the user enters something that isn't a number, raise a ValueError.

Okay.

B, if the user tries to divide by zero, raise a ZeroDivisionError.

C, the programme should keep asking the user for valid input until both errors are avoided.

Interesting.

I think we've done something similar, haven't we? So take your time.

You probably would want to pause this at this point or even rewind it to see what we've done previously and then write the programme that is described here.

Good luck.

So this is a possible answer.

Let's have a look through this.

So we start off by writing the while True.

Okay, and then we're gonna try, okay, so we put number one and number two as variables here.

And the user is going to enter the values for num1 and num2 on line three and four.

And then line five, we've got the result, which is the variable that we're going to store the sum of num1 divided by num2.

Great stuff.

And then on line seven, we've got print the result of num1 divided by num2 is result, so we're displaying the result, the value of the result.

And then we've got break.

That's great if that all works.

That's it, the end of the programme.

However, on line 10, we've got the except ValueError.

So again, we've got the prompt on line 11 which says Error: Please enter whole numbers only.

So if they enter the character, the user enters a character, it will bring that prompt up.

And then we've got on line 12, the except ZeroDivisionError where we prompt the user again, you can't divide by zero, try again.

So it will loop back to the try block on number two all the time until it reaches that break and it's all correct on line eight.

Well done.

You've come to the end of the lesson.

You've done really, really well.

In summary, robust programmes are designed to cope with unexpected input or behaviour.

Robust programmes are important as you cannot always predict what a user will enter into a programme.

And exception handling can be used to prevent the programme from crashing by catching and responding appropriately.

Well done.

I've really enjoyed this lesson.

I hope you have.

It's amazing that you've got this far and hopefully you'll take this on board in your programme.

Well done.