Section 7.6 Printing Intermediate Results
In this textbook we provide the codelens tool so that you are able to step through the program and watch what happens each time a line is evaluated by the Python interpretor. What if you didn’t have codelens though, what would you do?
In that case, print statements are your best friend. They can show you what the value of something is. This is especially helpful in the case of writing for loops or accumulating a value. If something is going wrong you can compare what you expect to happen to what is actually happening.
Say we weren’t sure what
num
was being assigned each time we iterated. One way to find out would be to add a print statement inside of the for loop to see.If we wanted to see what was happening to
tot
as well, we could print that out in the for loop as well!Finally, if we wanted to make these numbers easier to understand, we could add more to the print statements to make them easier to read!
You have attempted of activities on this page.