Now the next tip has been the thing that you've probably been screaming out for.
Why don't we use print? And indeed you are correct.
Print is basically your best friend. Now as developers
I know we don't have a lot of friends,
but at least we can rely on print to be our trusty friend.
Do you have heartbreak? Use print. Now print probably can't help you with your
relationship issues or family issues,
but it can help you debug your code,
which is a pretty decent thing for it to do.
So let's go ahead and comment out the previous block and let's uncomment this
one. And now I'm sure you're pretty much dying to use print to solve these
debugging issues. Why Angela? Why haven't you been using it before? Well,
I've been saving it for this perfect moment.
Take a look at the code and see if you can use print as your friend to help you
solve the issue. Pause the video now and give that a go.
So here, we've got
a very simple program that figures out the number of words in a book. Pages
starts out zero, words starts out at zero,
and we have to input the number of pages in our book and the number of words in
our book.
And then we multiply the pages by the words to calculate the total number of
words in the book and then we print it out. But if we have a go,
you can see that let's say we have, um,
45 pages making a booklet and we have 250 words per page and we get 0.
So the output that's supposed to tell us the total number of words in the book
ends up being 0. So what's going on here?
How can we debug this? Let's go ahead and use print.
What are our assumptions? Well,
we're actually getting the number of pages and the number of words correct
so that we can calculate this.
Why don't we add some print statements before the final one? Let's print
what's the actual value of some of our variables that we're getting from the
user. For example, I could say, um,
the pages equals and then insert the pages variable in here in an fstring.
And I can also print my other variable in an fstring,
which is the word per page variable.
And this is equal to let's insert that variable as well.
And now when I run my code,
I've got my print statements going and I can test it out.
So we still got 45 pages and then 250 words per page.
Now you can see that my variables are logged. So this one,
this pages variable does equal 45. So it captured my data correctly.
But the word per page equals 0,
which is not what I entered at all. I entered 250
so I was expecting this to be 250.
So now using print, my best friend,
I've managed to narrow down the problem.
It's something to do with this variable.
So let's take a look at what's going on here. Well,
I've spotted the problem,
have you? Pause the video and see if you can fix this code
so the final total number of words actually gets printed when you run the code.
Pause the video now. All right,
we've narrowed down to this one line of code that's probably broken.
And if you look closely, you'll see that instead of a single equal sign
which means assignment,
this variable should be equal to this value.
We had two equal signs.
This means does this variable equal this value?
And this actually gets evaluated and becomes either true or false.
Now, in this case, it's actually false. Words per page starts out being equal to
zero, and then it becomes equal to 250.
So if this line of code is false, then that's just left as it is,
it doesn't actually affect anything.
And this total word is still using zero, which is why it's printing out zero.
So now that we've identified the issue, all we have to do to fix it is that.
And if we run our code again,
you can see it works exactly as it should do.