Driving from Chicago to Dallas

As an example, imagine that you were planning to drive from Chicago to Dallas. If you know how many miles-per-gallon your car gets, and how many miles it is, you can estimate the number of gallons it will take.

Use the CodeLens below to trace through what it would look like for a computer to execute this program:

(Chicago_2_Dallas)

Now, if we know the average cost of a gallon of gas, we can figure out the cost of the trip from Chicago to Dallas in your car.

(Chicago_2_Dallas_Cost)

What we’re doing above is tracing a program. Normally, we run a program – tell the computer to execute each step of the program as fast as possible. When we do that, we don’t see individual variable values, as we can above. We can check values (including the values for variables) by printing them. The function print can take an input (a variable name inside of parentheses) whose value will be displayed. The print function can also print a string (like "Cost to get from Chicago to Dallas") which is a sequence of characters inside a pair of double quotes as seen in line 6. It will print the exact contents of the string. This is useful for explaining the values that you are printing.

Press the run button below to see this program run at full speed.

How does this program work? Try pressing the audio tour button button above to listen to an explanation of the program.

Try editing the program above and running it to answer this question:

    csp-3-5-1: If the cost per gallon drops to $3.45, can we drive from Chicago to Dallas for less than $90 in gas?
  • Yes
  • Yes, the cost would be $89.86 (which you knew by editing the program above and running it)
  • No
  • Try it -- it's just under $90.
  • We should fly instead.
  • You might be right, but figure out the cost by editing the above program
    csp-3-5-2: What would be printed by print("cost_per_gallon") if this line was added to the end of the last program?
  • 3.45
  • This would be true if it was printing the value of original variable.
  • 3.65
  • This would be true if it was printing the value of the variable after you changed it to figure out the previous question.
  • cost_per_gallon
  • Since cost_per_gallon is in double quotes it is a string, and it will print out those exact characters.
Next Section - Following the Ketchup Ooze