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

Converter Program

We will begin by writing a program similar to the one in the first part of Chapter 2. Here is the problem:
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: Input, Process, Output (IPO).

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 miles
Above, we are missing an important part-- the formula to calculate miles from kilometers. By typing 1 km = miles into google, we find that 1 km = 0.621371 miles. So, we can add that to our file:
	Input the distance in kilometers (call it km)
	Calculate the distance in miles as 0.621371*km
	Output miles
If you have not done so already, now would be a good time to save your work as the file, km2miles.py.

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 miles
Next, 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():
	#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():
	#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 main() function.

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():
	#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():
	#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.

Miles-Kilometer Chart

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.592339
Currently, 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*km
Save this program as km2milesChart.py and run it. What happens? What is missing from the loop?

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 km and miles and a string "\t". There are several keys on the keyboard whose are represented by special codes. One such key is TAB; if you want a TAB, you write \t in your string.

With the remaining time, work on the homework problems.