CS 170 Lab 2: Modifying a Python Program
This is a lab exercise to help you get started with editing Python
code to produce programs that solve the problems you need them to
solve. It is very common in programming to start with an existing
program and modify it, rather than writing code from scratch to solve
a problem.
- Launch Windows Explorer. View the
Y:\CS170 subdirectory that you (I hope) created for the first lab.
- Save the file convertTemp.py to your CS
170 directory.
- Launch Idle on this file. You do this by right-clicking on the
file and choosing Edit with IDLE from the menu. This will
open two windows, one with the text of the program and one to run
Python. (If this is still not working, you will need to open Idle
(via Python3.5) from the Start menu, then Open the file that you saved.)
- You might try running this program a few times, just to see what
it does. To do this, choose Run Module from the Run
menu.
- Your main task is to produce a new program, called convertDist.py
that, instead of converting temperatures from Celsius to Fahrenheit
will convert distances from Kilometers to Miles. You will want to use
Save As. . . once to save your file with the new name, then use Save
to regularly save your work. You need to change all variable names,
prompts and printed statements to reflect the new purpose of your
program. You will find it helpful to know that
miles = 0.6214 * kilometers
- When you have finished modifying your program, you can run
it. To do this choose Run Module from the Run menu. If you
made any mistakes in your modifications, you will get some kind of
error message. If you need help interpreting an error message, please
ask. Otherwise you
will be transferred to the Python Shell window and your program will
execute. One piece of data you should use to test your program is
47.2 kilometers, which (as we all know) is 29.33 miles. Test your program on other
data as well to make sure the results are plausible. What happens
when you enter 0 kilometers?
- When you are able to successfully run your program, you are
probably done. Go back to the window where you typed in the program.
Make sure that you don't want to change anything, then choose
Print Window from the File menu. This will print your file
to the default printer, which in the lab will be the printer in the
room. Be sure that your name is included in the file you print.
- You should hand in a printout of your file.