1
00:00:00,150 --> 00:00:04,320
Now the next tip has been the thing that you've probably been screaming out for.

2
00:00:05,010 --> 00:00:10,010
Why don't we use print? And indeed you are correct.

3
00:00:10,200 --> 00:00:14,220
Print is basically your best friend. Now as developers

4
00:00:14,250 --> 00:00:15,870
I know we don't have a lot of friends,

5
00:00:16,230 --> 00:00:20,220
but at least we can rely on print to be our trusty friend.

6
00:00:21,120 --> 00:00:25,650
Do you have heartbreak? Use print. Now print probably can't help you with your

7
00:00:25,650 --> 00:00:28,650
relationship issues or family issues,

8
00:00:28,920 --> 00:00:32,100
but it can help you debug your code,

9
00:00:32,430 --> 00:00:34,590
which is a pretty decent thing for it to do.

10
00:00:35,280 --> 00:00:40,280
So let's go ahead and comment out the previous block and let's uncomment this

11
00:00:40,920 --> 00:00:45,690
one. And now I'm sure you're pretty much dying to use print to solve these

12
00:00:45,690 --> 00:00:49,560
debugging issues. Why Angela? Why haven't you been using it before? Well,

13
00:00:49,560 --> 00:00:51,990
I've been saving it for this perfect moment.

14
00:00:52,710 --> 00:00:57,710
Take a look at the code and see if you can use print as your friend to help you

15
00:00:58,410 --> 00:01:02,070
solve the issue. Pause the video now and give that a go.

16
00:01:04,430 --> 00:01:05,630
So here, we've got

17
00:01:05,780 --> 00:01:10,780
a very simple program that figures out the number of words in a book. Pages

18
00:01:11,690 --> 00:01:14,960
starts out zero, words starts out at zero,

19
00:01:15,440 --> 00:01:20,440
and we have to input the number of pages in our book and the number of words in

20
00:01:20,450 --> 00:01:21,260
our book.

21
00:01:21,260 --> 00:01:26,260
And then we multiply the pages by the words to calculate the total number of

22
00:01:26,330 --> 00:01:30,350
words in the book and then we print it out. But if we have a go,

23
00:01:30,380 --> 00:01:32,990
you can see that let's say we have, um,

24
00:01:33,020 --> 00:01:38,020
45 pages making a booklet and we have 250 words per page and we get 0.

25
00:01:40,940 --> 00:01:44,900
So the output that's supposed to tell us the total number of words in the book

26
00:01:45,320 --> 00:01:47,990
ends up being 0. So what's going on here?

27
00:01:48,860 --> 00:01:53,330
How can we debug this? Let's go ahead and use print.

28
00:01:53,810 --> 00:01:55,820
What are our assumptions? Well,

29
00:01:55,880 --> 00:01:59,300
we're actually getting the number of pages and the number of words correct

30
00:01:59,330 --> 00:02:00,890
so that we can calculate this.

31
00:02:01,310 --> 00:02:05,720
Why don't we add some print statements before the final one? Let's print

32
00:02:05,750 --> 00:02:10,580
what's the actual value of some of our variables that we're getting from the

33
00:02:10,580 --> 00:02:14,030
user. For example, I could say, um,

34
00:02:15,200 --> 00:02:20,200
the pages equals and then insert the pages variable in here in an fstring.

35
00:02:22,730 --> 00:02:27,560
And I can also print my other variable in an fstring,

36
00:02:27,680 --> 00:02:31,400
which is the word per page variable.

37
00:02:31,910 --> 00:02:36,910
And this is equal to let's insert that variable as well.

38
00:02:37,850 --> 00:02:40,190
And now when I run my code,

39
00:02:40,220 --> 00:02:44,180
I've got my print statements going and I can test it out.

40
00:02:44,300 --> 00:02:49,300
So we still got 45 pages and then 250 words per page.

41
00:02:50,030 --> 00:02:54,680
Now you can see that my variables are logged. So this one,

42
00:02:54,680 --> 00:02:59,000
this pages variable does equal 45. So it captured my data correctly.

43
00:02:59,410 --> 00:03:02,080
But the word per page equals 0,

44
00:03:02,320 --> 00:03:06,400
which is not what I entered at all. I entered 250

45
00:03:06,490 --> 00:03:08,950
so I was expecting this to be 250.

46
00:03:10,120 --> 00:03:13,030
So now using print, my best friend,

47
00:03:13,450 --> 00:03:15,190
I've managed to narrow down the problem.

48
00:03:15,250 --> 00:03:17,590
It's something to do with this variable.

49
00:03:17,740 --> 00:03:21,220
So let's take a look at what's going on here. Well,

50
00:03:21,550 --> 00:03:23,200
I've spotted the problem,

51
00:03:23,290 --> 00:03:27,310
have you? Pause the video and see if you can fix this code

52
00:03:27,460 --> 00:03:31,840
so the final total number of words actually gets printed when you run the code.

53
00:03:32,440 --> 00:03:35,650
Pause the video now. All right,

54
00:03:35,680 --> 00:03:40,000
we've narrowed down to this one line of code that's probably broken.

55
00:03:40,900 --> 00:03:45,790
And if you look closely, you'll see that instead of a single equal sign

56
00:03:45,820 --> 00:03:47,350
which means assignment,

57
00:03:47,740 --> 00:03:51,160
this variable should be equal to this value.

58
00:03:51,820 --> 00:03:53,920
We had two equal signs.

59
00:03:54,370 --> 00:03:58,660
This means does this variable equal this value?

60
00:03:59,230 --> 00:04:03,820
And this actually gets evaluated and becomes either true or false.

61
00:04:04,330 --> 00:04:08,770
Now, in this case, it's actually false. Words per page starts out being equal to

62
00:04:08,770 --> 00:04:11,710
zero, and then it becomes equal to 250.

63
00:04:12,520 --> 00:04:17,110
So if this line of code is false, then that's just left as it is,

64
00:04:17,140 --> 00:04:18,880
it doesn't actually affect anything.

65
00:04:19,360 --> 00:04:24,190
And this total word is still using zero, which is why it's printing out zero.

66
00:04:24,790 --> 00:04:29,440
So now that we've identified the issue, all we have to do to fix it is that.

67
00:04:30,310 --> 00:04:31,870
And if we run our code again,

68
00:04:34,860 --> 00:04:37,770
you can see it works exactly as it should do.

