WEBVTT

0
00:00.440 --> 00:07.430
Now, the next step is to think about reproducing the bug that you've encountered, because when you

1
00:07.430 --> 00:13.850
encounter it once but you don't encounter it the next time, that becomes a really difficult bug to

2
00:13.850 --> 00:14.570
fix.

3
00:15.050 --> 00:18.110
In this section, we're going to try and reproduce the bug.

4
00:18.110 --> 00:23.900
So when we run the code, sometimes it will work and it will print out one of the dice_images from this

5
00:23.900 --> 00:27.500
list, but occasionally you will get an error.

6
00:28.040 --> 00:34.380
These types of bugs are really difficult because you might test your code only once or twice and it

7
00:34.380 --> 00:35.400
looks all fine.

8
00:35.400 --> 00:39.030
It works, but then occasionally you get an error.

9
00:39.030 --> 00:42.540
And the important thing is to reproduce that error.

10
00:42.540 --> 00:45.510
When does that error actually happen?

11
00:45.810 --> 00:54.880
And based on that knowledge, we can fix our code. Try to reproduce the bug yourself and try to notice

12
00:54.880 --> 01:01.660
when it happens, and see if you can change the code so that it always produces this error.

13
01:07.210 --> 01:16.480
We've got a list of dice_images which are just emojis, and we've also got this random number, dice_num,

14
01:16.480 --> 01:18.290
between 1 and 6.

15
01:18.530 --> 01:24.710
Now, when we try to pick out of our list, occasionally, we get an error.

16
01:24.950 --> 01:30.950
Now we have to notice when the error occurs, and to reproduce the bug, we have to figure out which

17
01:30.950 --> 01:33.710
of these numbers is actually causing the problem.

18
01:33.710 --> 01:38.660
So we know that this is a random number between 1 and 6.

19
01:38.660 --> 01:44.910
And if we check the documentation, it tells us that randint() works a little bit differently from range(),

20
01:44.910 --> 01:52.050
it will return a random integer in the range of A and B, including both end points.

21
01:52.050 --> 01:58.200
So in our case, both 1 and 6 could be generated and it could be any number.

22
01:58.200 --> 02:03.000
So if that was equal to 1 well does it generate an error?

23
02:03.030 --> 02:03.540
No.

24
02:03.540 --> 02:06.150
It just picks out this particular item.

25
02:06.150 --> 02:09.430
But what if it was 2, or 3, or 4?

26
02:09.430 --> 02:12.160
Or what if we tested 6?

27
02:12.310 --> 02:17.530
Well, now every time we hit run, you can see we get an error.

28
02:17.680 --> 02:24.670
And now that we've got our error to show up consistently, it's a lot easier to debug because we know

29
02:24.670 --> 02:30.640
that the 'IndexError: list index out of range,' happens when this dice_num is 6.

30
02:30.640 --> 02:40.220
So when we go back to our previous code, well, we can't use 6 in this list because lists start counting

31
02:40.220 --> 02:41.240
from 0.

32
02:41.240 --> 02:46.370
So to get this Dice 1, we need dice_num to be 0.

33
02:46.370 --> 02:53.180
And then this is 1, 2, 3, 4, 5, and 6 is somewhere out here and it doesn't exist.

34
02:53.210 --> 02:59.850
Now go ahead and fix this code so that all the dice_images are represented and we never see this error

35
02:59.850 --> 03:00.360
again.

36
03:02.370 --> 03:02.850
All right.

37
03:02.850 --> 03:06.240
So that is as simple as shifting down these numbers.

38
03:06.240 --> 03:13.200
So instead of making a random number between 1 and 6 we actually want a random number between 0 and

39
03:13.200 --> 03:13.710
5.

40
03:13.710 --> 03:19.380
And now no matter how many times we run our code, we're never going to get that error show up again.