Imagine the following trivial problem that you, as a programmer, have to solve:
A car is traveling 125 km/hr for 3 hours. How many km does the car travel in that time?
Here is a sample solution:
print("The car travels 375 km.")
This is a trivial solution that answers the question, but is utterly useless for anything else. (You certainly would get no marks for it in this course!) The output can be improved by echoing back the original problem information:
print("A car traveling at 125 km/hr for 3 hours travels 375 km.")
A slight improvement - now we know what the starting values were - but the biggest problem is that the programmer has done the work the computer is supposed to do. This violates the old adage:
A good programmer is a lazy programmer.
(Note that the reverse is not necessarily true.) This programmer has done the math for the computer! An improvement:
print(f"A car traveling at 125 km/hr for 3 hours travels {125 * 3} km.")
By using 125 * 3
in the print
function the
programmer has forced the computer to do the math. (The formatted printing
allows us to do this, but note, we do not want you to do so in this
course.) This is still not adequate.
What if we use variables instead?
speed = 125
time = 3
distance = speed * time
print(f"A car traveling at {speed} km/hr for {time} hours travels {distance} km.")
This is still an incredibly poor program. Although it does provide a solution to the given problem it is very narrow in scope. It is useful for a car traveling at 125 km/hr for 3 hours, but is of no use whatsoever for different speeds and/or times. Here is a vastly improved version:
speed = float(input("Speed of car (km/hr): "))
time = float(input("Time traveled (hr): "))
distance = speed * time
print(f"A car traveling at {speed} km/hr for {time} hours travels {distance} km.")
This version of the program not only provides a solution for the given problem, it provides a solution for any similar problem that differs only in speed and/or time. It is a generalized program.
A further improvement is to move the calculation to a function that can be reused in multiple places. Now we have:
def calc_distance(speed, time):
"""
-------------------------------------------------------
Calculates and returns a distance travelled.
Use: distance = calc_distance(speed, time)
-------------------------------------------------------
Parameters:
speed - current speed (float >= 0)
time - time spent travelling (float >= 0)
Returns:
distance - distance travelled (float)
------------------------------------------------------
"""
distance = speed * time
return distance
speed = float(input("Speed of car (km/hr): "))
time = float(input("Time traveled (hr): "))
distance = calc_distance(speed, time)
print(f"A car traveling at {speed} km/hr for {time} hours travels {distance} km.")
where the calc_distance
function can be used anywhere we need
to calculate a distance. Note that the function is unit agnostic - it
works for km and hours as well as furlongs and fortnights.
(Yes, this function is arguably too trivial to bother with given the simplicity of its calculation, but reusing a more complex function - say a mortgage calculation - would repay the effort in simplicity of reuse.
A few other notes on the last program style:
distance
, is calculated as a
separate variable. It is poor form to do calculations inside the print
function. A calculation performed inside a print
cannot be
reused, but a calculation saved to a variable can.