Lab 2:  First Programs

Lab Index  Course Home Page

Go to your Python directory as described in Lab 1.  If this is still on D: drive, you may need to recreate it and recopy the Idle shortcut icon again from the web

Whenever you are done in a University Lab:  In case you do not finish this lab, I am putting the instructions here for how to save the work you have so far.  Come back and do what you need to when you leave, as described in this paragraph.  If you are working on your own computer or a flash drive, you are all set -- just take your possessions with you.  On the other hand, if you have your files in a directory on D: drive, that will disappear whenever the machine reboots and should not be left for others to copy in any case.  You need to save your files.  If you have a floppy disk inserted, you can just drag the whole directory to the floppy.  If you do not have any local storage medium, you need to store files somewhere you have access to.  You could send yourself email with attachments and retrieve it later, or you could put files in the Blackboard Digital Dropbox and recover them later.  When you are sure that has succeeded with all the data you want to keep, delete your directory on D: drive.  When you come back next time you will need to recreate your directory on D: drive with the necessary files, and pick up where you left off.

Task 1, The Classic First Program

First you want to open Idle in your Python directory, where you will store program files.

This time you want the edit window, not the command shell.  If you get the command shell, then go to the File menu ->  New Window

Type the following:

#
# authors: Author1, Author2
#
print ‘Hello world’

Look at your text:  you should see a single color (black probably).  

Save the file with the File menu -> Save, and then enter the file name hello.py.  Python program files should always be given a name ending in ".py".  

If you look back in the editor now, you should see a change:  your code went from one color to color coded.  The editor will color different parts of Python syntax in special colors if it knows the file is a Python file, but until you provide a file name ending in ".py", the editor does not know you mean it to be a Python file (as opposed to a vanilla plain text file).  When you start a new blank document in the future, you are encouraged to save the blank document to a file ending in ".py"immediately, and then all the text you enter will be color coded.

Now that you have a complete, saved program, choose Run menu -> Run Module.  You shoul see the program run in the Python Shell window.

You just wrote and executed a program. Unlike when you use the shell, this code is saved to a file in your Python folder. You can open and execute the file any time you want.  (In Idle, use File->Open.)

If you are working on one person's computer or a flash drive one person will obviously have a copy.  One partner will not, or both will not if you are working on D: drive.  Practice making sure everyone has a personal copy.  If your work is on D: drive, for each person copy to a floppy or send an email attachment or copy to the Blackboard Digital Dropbox.  There should be multiple USB ports, so if both partners have USB drives, you can easily copy from one to the other.  It is also direct to copy from flash drive to floppy.

Task 2, Conversions

Write a program to convert Farenheit to Centigrade (backwards from the example in the book).  Remember to multiply and to use (5.0/9.0) instead of (5/9).   Why?  If you forget, enter 5.0/9.0 in the shell and then 5/9.  Start a new file.  When you are finished, save it to a new name.  Then go to the Edit window of the file you want to run, and select menu Run -> Run Modlule

Responding in an input statement in the Idle Python Shell:

When you see the input prompt in the shell window, make sure you put your cursor at the end of the line with the prompt, and type your response and <enter>.  The program waits for you and continues after you press <enter>.

Test your program at least three times to see that it is correct at the following three temperatures:

Body Temperature: 98.6F = 37.0C
Water Boils: 212F  = 100.0C 
Water Freezes: 32F = 0.0C

Killing a program in idle and running again

You may decide in the middle of running a program that you want to make a change and not complete execution.  In this situation you need to go to the Python Shell window and select the Shell menu -> Restart Shell.  There can be a problem though:

Things will not work right if your last program was expecting keyboard input when you stopped it.  You will need to click in the Python shell window and type <enter> at least once and maybe a couple of times in this case, preferably before you start your next program.  If you get the >>> prompt after pressing return, you have pressed return enough times.  A separate thread of execution waits for keyboard input, and it is not stopped by Restart Shell.  This is highly annoying, but easily fixed IF you remember it could be the reason your next program does nothing (still waiting for the old program's input).  If you stop a program and it MIGHT be waiting for input, nothing is lost by hitting <enter> until you see the >>> prompt.

When your program seems to work, make sure you and your partner keep a copy for after you leave the lab.

Task 3: Grade School Division

If you have not done so recently, switch who is "driving".  

When I was in grade school we would describe the result of an integer division like "14 divided by 3", as "Quotient 4 with a remainder 2"  Write a program that asks the user for a positive integer and another positive integer to divide it by, and then states the result in a form like above.

Have both partners save the program.

It is most efficient to get your lab checked by a TA, face to face.  Please try to do it either on the day of the lab or the next day of class, before, during or after class.  If you have either finished late or the TA's are busy with people who have not finished yet until it is time for you to leave the next day, then turn in your lab files through Blackboard.  

If you use Blackboard, please modify the the files as follows before uploading:  change the filenames to include the login ID of at least one partner and still identify the program.  For instance I might submit hello-aharrin.py, convert-aharrin.py, and division-aharrin.py.  Also make sure each file has a comment at the top including each partner's name.  The choice of when to fall back on Blackboard will continue into later labs, but for later labs I will suggest a way to create and submit only a single file, even if you have multiple programs!


Lab Index  Course Home Page