Variables Only Hold Values

Okay, now that we can get input from the human and do calculations with it, I want to call attention to something that many of my students get confused about. The following code should run, but it probably will not work the way you expect.

I have intentionally made a logical error in the code. It is not a problem with the syntax (the part of the code the compiler cares about), and it is not a runtime error like you get when the human types a float when the input() function is expecting an int. This logical error is a flaw with how I have designed the flow of instructions, so that the output is not what I was trying to accomplish.

sequencing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from __future__ import print_function
input = raw_input

# THIS CODE IS BROKEN UNTIL YOU FIX IT

price = 0
salesTax = price * 0.0825
total = price + salesTax

price = float(input("How much is the purchase price? "))

print("Item price:\t", price)
print("Sales tax:\t", salesTax)
print("Total cost:\t", total)

What You Should See

How much is the purchase price? 7.99
Item price:  7.99
Sales tax:   0.0
Total cost:  0.0

Are you surprised by the output? Did you expect the sales tax on $7.99 to show something like $0.66 instead of a big fat zero? And the total cost should have been something like $8.65, right? What happened?

What happened is that in Java (and most programming languages), variables can not hold formulas. Variables can only hold values.

Look at line 7. My students sometimes think that line stores the formula price * 0.0825 into the variable salesTax and then later the human stores the value 7.99 into the variable price. They think that on line 13 when we print out salesTax that the computer then "runs" the formula somehow.

This is not what happens. In fact, this program shouldn't have even run. The variable price doesn't even have a proper value on line 7. The only reason it does have a value is because I gave it one on line 6.

So then on line 10 the value the human types in doesn't initialize price; price already has an initial value (0). But the value the human types in (7.99 or whatever) does get stored into the variable price here. The 0 is replaced with 7.99.

From line 6 through 8 the variable price contains the value 0. When line 10 begins executing and while we are waiting for the human to type something, price still contains 0. But by the time line 10 has completed, whatever the human typed has been stored into price, replacing the zero. Then from line 11 until the end of the program, the variable price contains the value 7.99 (or whatever was typed in).

So with this in mind, we can figure out what really happens on line 7. Line 7 does not store a formula into salesTax but it does store a value. What value? It takes the value of the variable price at this point in the code (which is 0), multiplies it by 0.0825 (which is still zero), and then stores that zero into salesTax.

As line 7 is beginning, the value of salesTax is undefined. By the end of line 7, salesTax holds the value 0. There is no line of code that changes salesTax (there is no line of code that begins with salesTax =), so that value never changes and salesTax is still zero when it is displayed on line 13.

Line 8 is similar. It takes the value of price right then (zero) and adds it to the value of salesTax right then (also zero) and stores the sum (zero) into the variable total. And total's value is never changed, and total does not somehow "remember" that its value came from a formula involving some variables.

So there you have it. Variables hold values, not formulas. Computer programs are not a set of rules, they are a sequence of instructions that the computer executes in order, and things later in your code depend on what happened before.

(It actually is possible, of course, to package up a formula and attach a name to it. Then using that name in your code will cause the formula to run each time it is referenced. We call it "function definition" and a "function call", but it's too complicated to discuss right now. And it isn't happening in this program, in any case.)

Study Drills

  1. Remove line 6, so that price no longer gets defined. What happens when you try to compile the code? Does the error message make sense? (Now put it back so that the program runs again.)

  2. Move the two lines of code that give values to salesTax and total so they occur after price has been given a real value. Confirm that the program now works as expected.

  3. Now that these lines occur after the variable price has been properly given a real value, try removing line 6 again. Does the program still give an error? Are you surprised?




©2017 Graham Mitchell