CIS 166 & CMP 167: Introductory Programming

Lehman College, City University of New York

Spring 2015

Today's lab will focus more on writing simple programs in Python.

Daniel has started to train for triathalons. Unfortunately, his bike computer only reads out distances in kilometers. Write a conversion program for him that takes as input kilometers and outputs the corresponding distance in miles.Our first step is to design the algorithm. It follows a standard pattern:

In a text window in IDLE, write out what the input is, what the process is, and what the output is.

Here is one possible design:

Input the distance in kilometers (call it km) Calculate the distance in miles as ? Output milesAbove, we are missing an important part-- the formula to calculate miles from kilometers. By typing

Input the distance in kilometers (call it km) Calculate the distance in miles as 0.621371*km Output milesIf you have not done so already, now would be a good time to save your work as the file,

Now, we are ready to convert our outline of the algorithm to Python. Begin by adding a comment to the beginning of the file:

# Lab 2: Converter Program # YourNameHere, February 2014 Input the distance in kilometers (call it km) Calculate the distance in miles as 0.621371*km Output milesNext, let's add in the function definition, function call, and comment our outline so that we can test as we go:

# Lab 2: Converter Program # YourNameHere, February 2014 def main(): print("Welcome to Daniel's calculator!") #Input the distance in kilometers (call it km) #Calculate the distance in miles as 0.621371*km #Output miles main()Make sure that there are no typos by saving and running your program (F5 or run in the Run menu).

Next, fill in the program line by line. First, ask the user for their input:

# Lab 2: Converter Program # YourNameHere, February 2014 def main(): print("Welcome to Daniel's calculator!") #Input the distance in kilometers (call it km) km = eval(input('Enter distance in kilometers: ')) #Calculate the distance in miles as 0.621371*km #Output miles main()Test to make sure that the program runs. Note that it will ask for the input but will do nothing else since there is only that one Python statement in the

Python cares about spacing and distinguishes between spaces and tabs. If you get an error above, check that each of the lines beneath the definition is indented using a TAB.

Now, process the input (ie do the conversion):

# Lab 2: Converter Program # YourNameHere, February 2014 def main(): print("Welcome to Daniel's calculator!") #Input the distance in kilometers (call it km) km = eval(input('Enter distance in kilometers: ')) #Calculate the distance in miles as 0.621371*km miles = 0.621371*km #Output miles main()Save and run your program again. It will ask for input but does not yet produce output.

Finally, add the print statement that shows the output:

# Lab 2: Converter Program # YourNameHere, February 2014 def main(): print("Welcome to Daniel's calculator!") #Input the distance in kilometers (call it km) km = eval(input('Enter distance in kilometers: ')) #Calculate the distance in miles as 0.621371*km miles = 0.621371*km #Output miles print("The miles are", miles) main()Save and run your program again. This time, it will produce output. Try it with some simple values to make sure that it gets the right answer. For example, if you input 0 km, the output should also be 0. If you input 1 km, you should output 0.621371 miles.

Our second program for today builds on the program above to make a table that gives kilometers and their corresponding kilometers:

km miles 0 0.0 1 0.621371 2 1.242742 3 1.864113 4 2.485484 5 3.106855 6 3.728226 7 4.349597 8 4.970968 9 5.592339Currently, our function asks us for input, but that's not needed here since we will always print out the first kilometers from 0 to 10 and their corresponding miles. For each conversion from kilometers to miles, we use the same formula. We could rewrite the formula 10 times, but it would be faster to use a loop:

#Lab 2, Chart of km-miles #YourNameHere def main(): # <-- Nothing inside the parenthesis, since no input is used for km in range(10): miles = 0.621371*kmSave this program as

For information to be displayed to the screen, we need to have a print statement:

#Lab 2, Chart of km-miles #YourNameHere def main(): for km in range(10): miles = 0.621371*km print(miles)Add the print statement above, and run again. What else is needed to produce the nice chart at the beginning of this section? Try adding to the print statement to duplicate the chart above.

How did you do? Here is the final version of the program that produced the initial chart:

#Lab 2, Chart of km-miles #YourNameHere def main(): print("km\tmiles") for km in range(10): miles = 0.621371*km print(km, "\t", miles) main()Note that the print() function call has 3 inputs: the two variables

If you finish early, you may work on the programming problems.