Debugging Arithmetic Expressions

The program below takes a length of 1427 inches. It was meant to print the length in yards, feet, and inches, then whether it is a whole number of feet, then its thousands digit. This lesson finds its mistakes and repairs them one line at a time.
length = 1427                    # a length in inches
yards = length // 36             # whole yards
left = length % 36               # inches left after the yards
feet = length // 12              # whole feet in what is left
inches = left % 12               # inches left after the feet
print(yards, feet, inches)
exact_feet = length // 12 == 0   # True when the length is whole feet
print(exact_feet)
print(length % 1000)             # the thousands digit
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.

A yard is 36 inches and a foot is 12. Run the program above, and as the lesson finds each mistake, repair that line in the program and run it again.

Logic errors

Python runs the program to its last line and prints
39 118 11
False
427
The correct output for 1427 inches is 39 yards, 1 foot, and 11 inches, then False, then 1. Two of the five values are wrong, and Python gave no message.
A syntax error would have stopped Python before the first line ran, and a run-time error would have stopped it at the line that failed. This program ran to the end, so its wrong values come from logic errors.

Three kinds of division

Each line that divides should be read against what it's meant to compute. The second line should compute a count of whole yards, and its // gives 39, the number of whole 36s in 1427. The third line gives the inches left over after those yards, and its % gives 23. Both lines use the right operator. A line that wanted the fraction, such as the length in feet as a decimal, would use / instead.

Carrying the leftover

The fourth line should count the whole feet in those 23 inches. It divides length instead, giving 118, the whole feet in the entire length. Of those, 117 are the 39 yards counted a second time.
The feet line has to work on what the yards line left over, so the line becomes feet = left // 12, which gives 1. The fifth line already used left, so its 11 was right.

A yes-or-no line

The line that sets exact_feet is meant to be True when the length is a whole number of feet. For 1427 it printed False, which is correct, since 1427 inches is 118 feet and 11 inches. One correct output doesn't show that a line is correct, though.
The length 1428 is exactly 119 feet, so the line should give True. It gives False, because 1428 // 12 is 119, and 119 isn't 0. The remainder tells the two lengths apart where // can't: 1427 % 12 is 11 and 1428 % 12 is 0.
The line becomes exact_feet = length % 12 == 0, which gives False for 1427 and True for 1428.

Reading off digits

The last line should print the thousands digit of 1427, which is 1, but it printed 427. Dividing by 10 splits a number before its last digit: 1427 % 10 is 7 and 1427 // 10 is 142. Dividing by 1000 splits it before the last three digits, and % keeps those three. That is the 427 the line printed.
The thousands digit is in the part that // keeps. For a four-digit number that part is the thousands digit alone, so one division reaches it. The line becomes print(length // 1000), which prints 1.

Testing the repaired program

The lengths 1427 and 1428 are defined inputs: each was chosen with the output it should produce worked out by hand.
Here is the program with all three repairs.
length = 1427                    # a length in inches
yards = length // 36 # whole yards
left = length % 36 # inches left after the yards
feet = left // 12 # whole feet in what is left
inches = left % 12 # inches left after the feet
print(yards, feet, inches)
exact_feet = length % 12 == 0 # True when the length is whole feet
print(exact_feet)
print(length // 1000) # the thousands digit (lengths under 10000)
With length = 1427 it prints 39 1 11, False, and 1, the output worked out at the start. With length = 1428 it prints 39 2 0, True, and 1, as expected for 119 feet, or 39 yards and 2 feet. Testing with 1427 alone would have passed the old whole-feet line, which also gave False for it. A yes-or-no line should be tested with a defined input for each answer it can give.