Sei sulla pagina 1di 3

Case Study: Converting Miles to Kilometers

Your summer surveying job requires you to study some maps that gives distances in
kilometers and some that use miles. You and your coworkers prefer to deal in metric
measurements. Write a program that performs the necessary conversion.
The first step in solving this problem is to determine what you are asked to do. You must
convert from one system of measurement to another, but are you supposed to convert
from kilometers to miles, or vice versa? The problem states that you prefer to deal in
metric measurements, so you must convert distance measurements in miles to kilometers.
Therefore, the problem input is distance in miles and the problem output is distance in
kilometers. To write the program, you need to know the relationship between miles and
kilometers. Consulting a metric table shows that one mile equals 1.609 kilometers.
The data requirements and relevant formulas are listed below. miles identifies the
memory cell that will contain the problem input and kms identifies the memory cell that
will contain the program result, or the problem output.
Data Requirements
Problem Input
# the distance in miles
Problem Output
# the distance in kilometers
Relevant Formula
1 mile = 1.609 kilometers

Next, formulate the algorithm that solves the problem. In this case, flowchart is being
used as the algorithm to solve the problem.

Read Distance_in_Miles

Convert the distance to kilometers

Distance_in_KM =
Distance_in_Miles * 1.609

Display Distance_in_KM


To implement the solution, you must write the algorithm as a python program. To do this,
you must first tell the python compiler about the problem data requirements that is,
what memory cell names you are using and what kind of data will be stored in each
memory cell. Next, convert each algorithm step into one or more python statements. If an
algorithm step has been refined, you must convert the refinements, not the original step,
into python statements.
Figure below shows the python program along with a sample execution. For easy
identification, the program statements corresponding to algorithm steps are in color as is
the input data typed in by the program user.

Miles-to-Kilometers Conversion Program
# Converts distance in miles to kilometers
# Conversion constant
KMS_PER_MILE = 1.609
# Input distance in miles
miles = 0
# Output distance in kilometers
kms = 0
# Get the distance in miles
print "Enter the distance in miles",
miles = input(":")
# Convert the distance to kilometers
kms = KMS_PER_MILE * miles
# Display the distance in kilometers
print "That equals ", kms
Sample Run
Enter the distance in miles :10.00
That equals 16.09
How do you know the sample run is correct? You should always examine program results
carefully to make sure that they make sense. In this run, a distance of 10.0 miles is
converted to 16.09 kilometers, as it should be. To verify that the program works properly,
enter a few more test values of miles. You dont need to try more than a few test cases to
verify that a simple program like this is correct.