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.
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.
So here we've got a very simple program that figures out the number of words in a book.
Words starts out at 0, 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, 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.
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.
What are 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 the pages equals and then insert the pages variable in here in an f-string.
And I can also print my other variable in an f-string, 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've still got 45 pages and then 250 words_per_page,
and 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 words_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 0,
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_words is still using 0, which is why it's printing out 0.
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.
And we can in fact delete this line to get rid of any other warnings.