Well,
back in the previous listing, we successfully created this particular program,
they would accept inputs positive numbers from the user.
It will study numbers in a list and it will continue to do so
until the user types in the word done. And at the end of it, it will print out.
If there was an even number,
it will print out what the first even number was and
will also eventually print out all the numbers in the list.
However,
one thing we discovered was that when we run the program and we added, let's say,
for example, instead of an actual integer like eight or nine,
we added, let's say 8.6
right?
When we press enter,
we ended up having this particular row me
a value arrow.
Now if we run the program again,
and instead of 8.5 I said, you know, iof I typed in letters instead I press enter,
you can see once again that we also have the exact same error.
And that's because our program has been created in such a way
that it is expected to receive positive numbers only
if we run the program one more time.
And I typed in, let's say minus six, which is a negative number and I pressed enter,
you can see
that it actually
kind of works in a way
it does accept
the negative six because it is an actual number. It just happens to be negative,
but
that still defeats the purpose because we only
want the user to type in positive numbers.
So it shouldn't even have accepted this negative six.
So we do have quite some issues with our particular program.
And we need to now introduce something known as the tri
and then the except block.
So the try block in Python is basically part
of what we call an Exception handling mechanism,
which would basically allow us to catch and handle errors that might occur
during the execution of a code.
So if an error occurs inside of the try block, the program will not crash.
Instead,
the error will be caught and the code inside
the associated except block will then be executed.
Keep in mind that it's not just about users
making a mistake whenever they type in their inputs.
It's also possible that a hacker will deliberately try to insert values that
the program will not accept in an effort to actually crash the program.
This is why this is why we need to always make sure that our programs,
whenever they're accepting inputs from the user,
we create and design them in such a way that they are able to handle
our own inputs from the user.
So
how are we going to now
catch any particular errors?
Well, I'm gonna come over here
where we're breaking. OK.
So
the reason why we're starting here is because this is where we're going to check
whether or not
the user has added
either the done word or an actual number.
So after break right after break, because break will check. OK.
Is it done if it's not done that it's now going to execute?
This is where we're now going to type in the words
or rather the word try
because this is where we want the program to catch
an error if there is.
So now it's going to be try and then colon
and now this right here,
we can simply
tab that.
OK?
Let me just press enter right here.
So
we are saying
right here
that
if the user types in done simply break the program, I'm sorry, break the loop.
However, if it's an actual number,
let us check to see if that number is positive.
So right now we have created the variable num we added the
user input to nu M.
So now let us check to see if it is positive.
The easiest way to check would be
to simply say if
the number
is less than zero,
this is the easiest way to check.
So if the number is in fact less than zero,
we're going to say print,
please enter a positive number.
OK. And now else
because after eve comes,
so now
if it was in fact a positive number,
we are now simply going to append,
OK, we're going to append
the
num variable two numbers
and that's it right here.
OK. And finally, because we have try,
we're going to close it out
by using the accept statement. So basically
what do, what he was saying? Hey,
if the value provided by the user was in fact less than zero,
then
print, please enter a positive number.
But then what if the user didn't type a number? What if they typed in letters
or strings basically, right?
So this is where the accept will now come in. So we're gonna say accept
value error.
OK? So if there's an actual error within the value itself, because it's not a number,
we are now going to say
uh print uh please enter
a valid number
and that's it right there. So
let us try and see if this will work.
So I'm gonna go ahead right now,
run the program.
OK?
So what I'm gonna do right now is I'm going to add negative six press enter.
You can see right here it says please enter a positive number.
But what if I type in a string like this? For example,
now it says please enter
a valid number.
You see how this works, right?
So let me walk you through it all over again,
basically right here where we're accepting the input from the user,
we want to check to make sure
that it's actually a positive number.
The best way is if that number is less than zero, then we know it's negative,
we can print out, please enter a positive number.
If on the other hand, it is greater than zero, we know it's a positive number.
We can go ahead right now and append the num to the numbers list.
And now
we will have to say, OK, but what if
there was an actual
error within the type of input that the user inserted?
Maybe it wasn't a positive number or maybe it wasn't even a negative number.
Maybe it was like a letters or words or something else entirely.
We can then say please enter a valid number.
That's where the accept comes into play.
So that's it for the try and except statements.
Thank you for watching the video. I will see you in the next class.