WEBVTT

0
00:00.950 --> 00:07.190
Now the next step and the next tip is pretty obvious when the editor is giving you an error, or when

1
00:07.190 --> 00:09.110
the console is giving you an error,

2
00:09.140 --> 00:11.840
fix the errors before you continue.

3
00:11.870 --> 00:17.450
Now, this is a little bit easier when you're dealing with errors in the editor, because you'll actually

4
00:17.450 --> 00:19.760
highlight you the line that's broken.

5
00:19.760 --> 00:21.920
So here we've got a print statement,

6
00:21.920 --> 00:27.650
and when we hover over it, it tells us that it expected an indented block.

7
00:27.650 --> 00:32.310
So that should be a good enough clue to tell us what's actually wrong.

8
00:32.340 --> 00:36.870
So if we go ahead and indent this block, then that error goes away.

9
00:36.870 --> 00:38.850
But that's not the only error,

10
00:38.880 --> 00:42.840
no, when you run this code, you still get another error.

11
00:42.840 --> 00:45.840
So let's say that I am a 12 year old girl,

12
00:46.650 --> 00:48.240
we get another error.

13
00:48.240 --> 00:56.280
And this error does not show up when we write the code, but it only shows up depending on what input

14
00:56.280 --> 00:58.770
we gave to this age variable.

15
00:58.770 --> 01:01.380
So now we have a different type of error.

16
01:01.380 --> 01:04.110
We have an error that's in the console.

17
01:04.440 --> 01:05.880
How do we solve this error.

18
01:05.880 --> 01:13.200
Well the easiest way is just to select the parts of the error that's not specific to your code, such

19
01:13.200 --> 01:17.700
as this part where it says 12 or our actual line of code.

20
01:17.700 --> 01:27.280
So if we go ahead and copy this part of our error and go into Google and paste it into the search

21
01:27.280 --> 01:33.640
box, and then we add our Python programming language, we should be able to figure out what is going

22
01:33.640 --> 01:34.240
on.

23
01:34.420 --> 01:38.110
So here is the exact same error that we have,

24
01:38.110 --> 01:42.610
and we have 17 answers explaining to us what could go wrong.

25
01:42.610 --> 01:50.680
And in this case it says the error message means that the string provided to this int conversion could

26
01:50.680 --> 01:55.180
not be parsed into an integer, so it couldn't be turned into an integer.

27
01:55.180 --> 02:01.120
And of course, the computer doesn't know how to turn 12 into the actual number 12, because you might

28
02:01.120 --> 02:05.890
have used even a different language, you might have used French, or you might have used German, right?

29
02:05.890 --> 02:07.780
So this doesn't actually work.

30
02:07.780 --> 02:12.560
And that should give us enough of a hint to know exactly what is going on.

31
02:12.560 --> 02:18.680
Now, of course, the easiest way is we could just tell the user to not do that, tell them to actually

32
02:18.680 --> 02:22.340
type numbers, and then we don't end up with that problem.

33
02:22.340 --> 02:26.030
But inevitably things might happen down the line,

34
02:26.030 --> 02:29.390
people might do something that is unexpected,

35
02:29.390 --> 02:36.860
so given that we have this weakness in our code, how can we actually fix our code to make it more durable?

36
02:37.550 --> 02:43.940
Well, one thing that I want to introduce you to for the first time here is the try-except block.

37
02:43.940 --> 02:48.200
So we can catch an exception using Python code.

38
02:48.200 --> 02:55.790
So the way that it works is we know that the potential error is something called a ValueError.

39
02:55.790 --> 03:03.540
So we can catch that error so that it doesn't crash our code and provide an alternative pathway for

40
03:03.540 --> 03:04.710
our code to go down.

41
03:04.830 --> 03:11.640
So the way that it works is we take the line of potentially dangerous code, which is this line where

42
03:11.640 --> 03:17.370
we convert a string that the user types in into an integer.

43
03:17.370 --> 03:20.970
And we've seen that that is what's causing this error here.

44
03:21.030 --> 03:25.800
So we take this line of code and we trap it inside a try block.

45
03:25.800 --> 03:28.200
So just the keyword try plus a colon,

46
03:28.200 --> 03:32.400
and then we indent this block of code to put it inside.

47
03:33.090 --> 03:35.100
So now we've got a try block.

48
03:35.100 --> 03:42.330
The next part is we need to add the 'except' keyword and then add in the exception that we want to catch.

49
03:42.330 --> 03:45.570
So in this case it's a ValueError that we're getting,

50
03:45.570 --> 03:47.820
and so that is what we're going to be catching.

51
03:47.820 --> 03:54.730
So now inside this except block, what we can do is we can say, well, what should happen if we end

52
03:54.730 --> 03:56.050
up with this error?

53
03:56.050 --> 04:01.660
So where the user types in something that we actually can't convert into an integer.

54
04:01.660 --> 04:06.130
Well let's first print something to tell them this is what's going on.

55
04:06.130 --> 04:11.560
For example, "You have typed in an invalid number.

56
04:11.680 --> 04:20.050
Please try again with a numerical response such as 15."

57
04:20.590 --> 04:24.910
So now we're giving them a little bit of feedback telling the user what's going on,

58
04:24.910 --> 04:30.010
and then we can just simply copy and paste this code here so that it runs again,

59
04:30.010 --> 04:32.260
and we give them another chance.

60
04:32.260 --> 04:37.990
So now let's run this code again and try with our previous offending code.

61
04:37.990 --> 04:43.190
And now you can see that we get our print statement giving us clear feedback

62
04:43.190 --> 04:44.150
what's wrong.

63
04:44.150 --> 04:47.600
And then it asks us how old are you?

64
04:47.600 --> 04:55.100
And now we can type in a number and continue our program as normal so it continues running instead of

65
04:55.100 --> 04:57.470
stopping our program and crashing it.

66
04:58.010 --> 05:06.710
But remember that solving the errors don't always solve all of your bugs like this.

67
05:06.740 --> 05:10.610
In this case, there are no errors with my code whatsoever,

68
05:10.610 --> 05:13.880
it's just not doing what I want it to do.

69
05:13.970 --> 05:19.160
And these are the most frustrating bugs to debug because nobody is helping you here.

70
05:19.160 --> 05:21.350
There's no errors that you can Google,

71
05:21.350 --> 05:23.810
there's no underlines that you can check,

72
05:23.810 --> 05:28.550
you actually have to rely on your skills as a programmer to fix this.

73
05:28.550 --> 05:35.370
So pause the video and see if you can fix the code so that instead of displaying the name of the variable,

74
05:35.370 --> 05:38.460
we actually get the value of the variable.

75
05:38.760 --> 05:39.900
Pause the video now.

76
05:43.650 --> 05:52.680
Now we of course know that this needs to be an f-string in order to insert that variable into this position

77
05:52.680 --> 05:54.300
in the curly braces,

78
05:54.450 --> 05:58.950
but if you didn't, then this would be a lot harder to debug.

79
05:59.520 --> 06:05.670
So experience comes in really handy with debugging, and the more bugs that you solve, the better you

80
06:05.670 --> 06:06.300
get at it.

81
06:07.080 --> 06:08.730
So go on to the internet.

82
06:08.730 --> 06:10.860
Try to help people on Stack Overflow.

83
06:10.860 --> 06:16.770
Go on to the discord channel for the course, try to solve other people's bugs and you will find yourself

84
06:16.770 --> 06:19.830
getting stronger and better day by day.