WEBVTT

0
00:00.110 --> 00:04.550
Now, the next tip has been the thing that you've probably been screaming out for,

1
00:04.670 --> 00:07.700
"Why don't we use print?"

2
00:07.910 --> 00:10.160
And indeed, you are correct,

3
00:10.160 --> 00:12.830
print is basically your best friend.

4
00:12.830 --> 00:19.040
Now, as developers, I know we don't have a lot of friends, but at least we can rely on print to be

5
00:19.040 --> 00:20.450
our trusty friend.

6
00:20.870 --> 00:22.250
Do you have heartbreak?

7
00:22.280 --> 00:23.270
Use print.

8
00:23.510 --> 00:30.770
Now print probably can't help you with your relationship issues or family issues, but it can help you

9
00:30.770 --> 00:32.240
debug your code.

10
00:32.240 --> 00:37.070
And now I'm sure you're pretty much dying to use print to solve these debugging issues.

11
00:37.070 --> 00:38.120
"Why, Angela?

12
00:38.120 --> 00:39.710
Why haven't you been using it before?"

13
00:39.710 --> 00:42.650
Well, I've been saving it for this perfect moment.

14
00:43.190 --> 00:49.190
So here we've got a very simple program that figures out the number of words in a book.

15
00:49.280 --> 00:56.980
Words starts out at 0, and we have to input the number of pages in our book and the number of words

16
00:56.980 --> 01:03.670
in our book, and then we multiply the pages by the words to calculate the total number of words in

17
01:03.670 --> 01:04.180
the book,

18
01:04.180 --> 01:05.500
and then we print it out.

19
01:05.500 --> 01:13.390
But if we have a go, you can see that...let's say we have, 45 pages making a booklet, and we have

20
01:13.390 --> 01:17.290
250 words per page and we get 0.

21
01:17.290 --> 01:23.100
So the output that's supposed to tell us the total number of words in the book ends up being 0.

22
01:23.100 --> 01:24.840
So what's going on here?

23
01:25.440 --> 01:27.660
How can we debug this.

24
01:27.660 --> 01:30.060
Let's go ahead and use print.

25
01:30.090 --> 01:37.560
Take a look at the code and see if you can use print as your friend to help you solve the issue.

26
01:37.590 --> 01:39.750
Pause the video now and give that a go.

27
01:41.580 --> 01:43.350
What are assumptions?

28
01:43.470 --> 01:48.630
Well, we're actually getting the number of pages and the number of words correct so that we can calculate

29
01:48.630 --> 01:49.080
this.

30
01:49.110 --> 01:52.680
Why don't we add some print statements before the final one.

31
01:52.680 --> 01:59.400
Let's print what's the actual value of some of our variables that we're getting from the user.

32
01:59.520 --> 02:10.390
For example, I could say the pages equals and then insert the pages variable in here in an f-string.

33
02:10.390 --> 02:19.630
And I can also print my other variable in an f-string, which is the word_per_page variable,

34
02:19.630 --> 02:22.480
and this is equal to,

35
02:22.510 --> 02:25.330
let's insert that variable as well.

36
02:25.540 --> 02:32.260
And now when I run my code I've got my print statements going and I can test it out.

37
02:32.260 --> 02:37.750
So we've still got 45 pages and then 250 words_per_page,

38
02:37.750 --> 02:41.650
and now you can see that my variables are logged.

39
02:41.650 --> 02:45.190
So this one, this pages variable does equal 45,

40
02:45.190 --> 02:47.170
so it captured my data correctly,

41
02:47.170 --> 02:53.170
but the words_per_page equals 0 which is not what I entered at all.

42
02:53.170 --> 02:54.460
I entered 250,

43
02:54.470 --> 02:57.110
so I was expecting this to be 250.

44
02:57.860 --> 03:03.290
So now using print, my best friend, I've managed to narrow down the problem.

45
03:03.290 --> 03:05.690
It's something to do with this variable.

46
03:05.690 --> 03:08.330
So let's take a look at what's going on here.

47
03:08.330 --> 03:11.330
Well, I've spotted the problem.

48
03:11.330 --> 03:15.410
Have you? Pause the video and see if you can fix this code

49
03:15.410 --> 03:20.060
so the final total number of words actually gets printed when you run the code.

50
03:20.450 --> 03:21.410
Pause the video now.

51
03:23.060 --> 03:23.690
All right.

52
03:23.690 --> 03:28.370
We've narrowed down to this one line of code that's probably broken.

53
03:28.520 --> 03:36.140
And if you look closely, you'll see that instead of a single equal sign which means assignment, this

54
03:36.140 --> 03:39.470
variable should be equal to this value,

55
03:39.560 --> 03:42.140
we had two equal signs.

56
03:42.140 --> 03:46.940
This means does this variable equal this value.

57
03:46.940 --> 03:52.070
And this actually gets evaluated and becomes either True or False.

58
03:52.070 --> 03:54.770
Now in this case it's actually False.

59
03:54.770 --> 03:57.470
Words_per_page starts out being equal to 0,

60
03:57.470 --> 03:59.870
and then it becomes equal to 250.

61
04:00.440 --> 04:04.550
So if this line of code is False then that's just left

62
04:04.550 --> 04:05.180
as it is.

63
04:05.180 --> 04:07.130
It doesn't actually affect anything.

64
04:07.140 --> 04:12.510
And this total_words is still using 0, which is why it's printing out 0.

65
04:12.540 --> 04:17.820
So now that we've identified the issue, all we have to do to fix it is that.

66
04:18.000 --> 04:20.130
And if we run our code again,

67
04:22.650 --> 04:26.130
you can see it works exactly as it should do.

68
04:27.420 --> 04:32.250
And we can in fact delete this line to get rid of any other warnings.