Hey, welcome to today's bonus example.
In the code experiment video you understood the difference between syntax errors and exceptions.
Now there is another type of issue which neither the grammar checker or the interpreter.
Nor the interpreter itself can capture.
These are more logical issues which only humans can spot in a program, and for that you need to use
different tools.
So to discover what these issues are.
Exactly.
I will create a program which asks the user to enter the dimensions of a rectangle, and the program
should output the area of the rectangle.
So that would be something like.
Width is equal to input.
You ask the user to enter the rectangle width.
Same for length.
And so you want to convert this into floats so that you capture a number instead of strings, so convert
the entire input function.
Put it inside, float inside the parentheses over the float function.
Event area would be equal to width times length.
And you print out the area.
So far, so good.
So you run the program, you enter for for width five for length, and you get 20.
But if the user enters four four with using a word instead of a number, then we would get this value
error.
So we want to tell the user, Oh, you need to enter an actual number.
So for that of course we use try and accept as you learned already.
So what we do is we indent all these lines and we put them inside.
I try.
Statements and.
So we expect an error to happen in any of these, and that is a value error.
If that happens, then we print out Please enter a number and note that we don't need the continuous
statements here because we are not inside a wire loop.
So that should be enough.
Let's try again for and we have this.
Please enter a number.
So far so good.
So we were able to successfully use try, accept.
To catch a potential error that the user might trigger.
But what if we don't want the user to enter?
For example, if we don't want the user to enter five four with and the same number for length.
So the program will say 25, but what if we want the program?
To display a message to tell the user, Oh, wait, that is a square.
And we we don't accept square calculations of various in our program.
So we want to to show a message to the user.
Of course, the try accept block would not accept that would not be able to to handle such a delicate
requirements because that that is not an error logically for the interpreter that is fine the interpreter
is simply multiplying two numbers, but this is an issue.
We should handle it in another way.
Now that is where if elif else conditional blocks come in handy again.
In this code, we would write the if conditional after we get the width and the length so we could check
if width is equal to length.
And if that's the case.
We exit the program with a message, we say.
That's looks like a square.
You could also live this empty and include a print function here and say here that that looks like a
square.
But this is.
Less code.
So this exit function now will.
Break the program will interrupt it and we will print out this.
String in the command line.
So let's try it.
Five.
And we get this.
That looks like a square.
The command line here in pie chart is also able to highlight this in red because it's being printed
out by that exit function.
So if you don't want that color, then you should use prints here.
And that put that as I told you inside print.
So what's going on here?
Well, this is what happens.
So we ask the user to answer some inputs.
If there is an error.
Then this will be executed.
So if there is a value error, actually that's then this will be executed.
If there is no error in here and in here as well.
Then here the if conditional will check those two values.
So you see that an if conditional can be used and it can be used without Elif and else.
Here.
It was not necessary to have leave or else.
So and if conditional can be used when you want to check or check values, when you want to check conditions
which do not produce errors.
So they are programmatically correct.
But.
The product is not acceptable from us.
So the requirements, for example, here, we don't want users for some reason to enter square dimensions.
So we tell them, Oh, we don't accept that.
That looks like a square.
So if conditionals do not catch errors.
They check values.
That's something I wanted you to keep in mind.
And that's what I was trying to demonstrate with this example.
So the difference between tri accept and if conditionals.
Thanks for following this and I'll talk to you later.