Dark theme

Using print


Although there are more sophisticated tools for debugging, almost everyone reaches first for one simple tool when debugging: print.

Printing values at different stages in your code tells you two things: 1) that the code runs to that point, and 2) that the value is as you think it should be. When issues are issues of logic, rather than syntax, it is hard to overestimate the value of these two things.


Here's a file with an issue in it: test1.py. Run it and you'll see that it runs fine but it comes back with the wrong answer; it should be 150.

The first thing is that we can decompose the problem into stages to make it easier to see where the problem is, so we could change:

for i in range(1,number_of_values):
    data_floats.append(float(data_strings[i - 1]))

to:
for i in range(1,number_of_values):
    string_value = data_strings[i - 1]
    float_value = float(string_value)
    data_floats.append(float_value)

If we then add some prints we can see what's going on it detail (notice the first print just helps us see where each loop starts and the last lets us know the loop has run successfully to the end):

for i in range(1,number_of_values):
    print("loop start")
    string_value = data_strings[i - 1]
    print(string_value)
    float_value = float(string_value)
    print(float_value)
    data_floats.append(float_value)
    print("loop end")


Here's the code: test2.py.

Here's the output:
loop start
10.0
10.0
loop end
loop start
20.0
20.0
loop end
loop start
30.0
30.0
loop end
loop start
40.0
40.0
loop end
100.0

Given our original data is: ["10.0", "20.0", "30.0", "40.0", "50.0"], it is clear that we're missing the last value. Adding a line to print i - 1 would confirm this (test3.py):

for i in range(1,number_of_values):
    print("loop start")
    print("i - 1 = ", i - 1)
    string_value = data_strings[i]
    print(string_value)
    float_value = float(string_value)
    print(float_value)
    data_floats.append(float_value)
    print("loop end")

Output:
loop start
i - 1 = 0
10.0
10.0
loop end
loop start
i - 1 = 1
20.0
20.0
loop end
loop start
i - 1 = 2
30.0
30.0
loop end
loop start
i - 1 = 3
40.0
40.0
loop end
100.0

This issue is that whoever wrote the code remembered that containers are indexed from zero, but forgot that ranges generate a series of number up to, but not including the second number. The number in position 4 in the container is never reached because number_of_values - 1 (== 4) is never generated. Overall, this code is confused. We could adjust it to:

data_strings = ["10.0", "20.0", "30.0", "40.0", "50.0"]
data_floats = []
number_of_values = 5

for i in range(1,number_of_values + 1):
    data_floats.append(float(data_strings[i - 1]))

print(sum(data_floats))

but better would be (test4.py):

data_strings = ["10.0", "20.0", "30.0", "40.0", "50.0"]
data_floats = []
for i in range(len(data_strings)):
    data_floats.append(float(data_strings[i]))

print(sum(data_floats))

Which will default to starting with i = 0 and will run to the final len(data_strings) - 1 position however we change the list in the future.


The key thing is that printing the values, and some extra prints to let us know we've started and ended the loops ok has helped us to identify the issue.

Here's a file for you to have a go at. Can you identify the issue using print statements: test5.py? Two things will help:

Remember: first decompose the problem. The find operation on strings find the position of letters and strings within a string, using the following syntax (where optional parameters are in italics):
str.find(str_to_find, start_location, end_location)

Secondly, as we'll see later in the course, every character has a numeric code that represents it. You can get these codes using, e.g.:
ord("A") # 65
And convert code to letters using, e.g.:
chr(97) # "a"
It is sometimes easier to use the codes that the characters; for example when dealing with non-UK/US characters not on a standard keyboard.

Decompose the issue into parts, and use print to test each one. Can you identify the issue?