Now the next step and the next tip is pretty obvious when the editor is giving you an error, or when
the console is giving you an error,
fix the errors before you continue.
Now, this is a little bit easier when you're dealing with errors in the editor, because you'll actually
highlight you the line that's broken.
So here we've got a print statement,
and when we hover over it, it tells us that it expected an indented block.
So that should be a good enough clue to tell us what's actually wrong.
So if we go ahead and indent this block, then that error goes away.
But that's not the only error,
no, when you run this code, you still get another error.
So let's say that I am a 12 year old girl,
we get another error.
And this error does not show up when we write the code, but it only shows up depending on what input
we gave to this age variable.
So now we have a different type of error.
We have an error that's in the console.
How do we solve this error.
Well the easiest way is just to select the parts of the error that's not specific to your code, such
as this part where it says 12 or our actual line of code.
So if we go ahead and copy this part of our error and go into Google and paste it into the search
box, and then we add our Python programming language, we should be able to figure out what is going
on.
So here is the exact same error that we have,
and we have 17 answers explaining to us what could go wrong.
And in this case it says the error message means that the string provided to this int conversion could
not be parsed into an integer, so it couldn't be turned into an integer.
And of course, the computer doesn't know how to turn 12 into the actual number 12, because you might
have used even a different language, you might have used French, or you might have used German, right?
So this doesn't actually work.
And that should give us enough of a hint to know exactly what is going on.
Now, of course, the easiest way is we could just tell the user to not do that, tell them to actually
type numbers, and then we don't end up with that problem.
But inevitably things might happen down the line,
people might do something that is unexpected,
so given that we have this weakness in our code, how can we actually fix our code to make it more durable?
Well, one thing that I want to introduce you to for the first time here is the try-except block.
So we can catch an exception using Python code.
So the way that it works is we know that the potential error is something called a ValueError.
So we can catch that error so that it doesn't crash our code and provide an alternative pathway for
our code to go down.
So the way that it works is we take the line of potentially dangerous code, which is this line where
we convert a string that the user types in into an integer.
And we've seen that that is what's causing this error here.
So we take this line of code and we trap it inside a try block.
So just the keyword try plus a colon,
and then we indent this block of code to put it inside.
So now we've got a try block.
The next part is we need to add the 'except' keyword and then add in the exception that we want to catch.
So in this case it's a ValueError that we're getting,
and so that is what we're going to be catching.
So now inside this except block, what we can do is we can say, well, what should happen if we end
up with this error?
So where the user types in something that we actually can't convert into an integer.
Well let's first print something to tell them this is what's going on.
For example, "You have typed in an invalid number.
Please try again with a numerical response such as 15."
So now we're giving them a little bit of feedback telling the user what's going on,
and then we can just simply copy and paste this code here so that it runs again,
and we give them another chance.
So now let's run this code again and try with our previous offending code.
And now you can see that we get our print statement giving us clear feedback
what's wrong.
And then it asks us how old are you?
And now we can type in a number and continue our program as normal so it continues running instead of
stopping our program and crashing it.
But remember that solving the errors don't always solve all of your bugs like this.
In this case, there are no errors with my code whatsoever,
it's just not doing what I want it to do.
And these are the most frustrating bugs to debug because nobody is helping you here.
There's no errors that you can Google,
there's no underlines that you can check,
you actually have to rely on your skills as a programmer to fix this.
So pause the video and see if you can fix the code so that instead of displaying the name of the variable,
we actually get the value of the variable.
Pause the video now.
Now we of course know that this needs to be an f-string in order to insert that variable into this position
in the curly braces,
but if you didn't, then this would be a lot harder to debug.
So experience comes in really handy with debugging, and the more bugs that you solve, the better you
get at it.
So go on to the internet.
Try to help people on Stack Overflow.
Go on to the discord channel for the course, try to solve other people's bugs and you will find yourself
getting stronger and better day by day.