$16
Computer Science 222
Learning
Objectives:
1. Understand the editing and execution phases of a computer
program.
2. Be able to execute a Python program using the IDLE integrated
development environment (IDE).
3. Interact with simple Python programs that produce output and do
arithmetic.
4. Enter new programs using the IDLE editor.
Directed
Activities:
1. Log on to the computer
using your Cougars account information.
a. Create a folder
named “Lab01” on the Desktop.
2. Open your WebCT account
for this course (e.g CSCI 222-L01).
You can get to
WebCT from the CofC webpage. Open a web browser and go to www.cofc.edu.
a. On the Homepage, select Labs, and select the one entitled Lab01 (click
on the title Lab01).
b. Under Assignment files, in the Files box, you will see two files.
Click on “First.py”.
c. In the View Files window, you’ll see the file name again. Click
on the circle in front of it and then press the Download button.
d. When asked, choose to "Save" the file and store it on
the Lab01 folder on your Desktop of your computer. This will place the file on
your computer's desktop. You should see the file named "First.py".
(Today we’ll work on the Desktop – starting soon, you will have your own disk
space available on these lab computers.)
3. Open and execute your
first Python program using IDLE.
a. Open the IDLE integrated development environment (IDE) by
double-clicking on the icon labeled IDLE.
b. On the IDLE toolbar, choose File,
then Open, and navigate to the
Desktop. Select the file named "First.py" (it may or may not show
an extension of "py") on the Desktop.
c. Double click (left mouse button) on the "First.py"
file and an editing window will open and display the code in that program. The
top of the edit window displays the name of the program ("First.py".)
d. Replace the text <your
name goes here with your name.
e. To execute the program, from the Run menu select Run module. Or simply press the F5 key.
This opens the Python Shell window that displays the output of the program –
the message "Programming is fun but is not a spectator sport!" (a
word to the wise).
f. Go back to the editor window. Change the message (stuff in
quotes) so that the program prints out "I look forward to learning to
control this computer through programming!" Save by pressing Control-S.
Execute your program, following instructions from part (e).
Demonstrate for
instructor.
You’ll be asked to upload this program to
WebCT later, so don’t change it any more.
4. Enter a new program
using the editor.
(Python is
case-sensitive, so type caps when you see caps and lower-case letters when you
see lower-case letters.)
a. From the File menu, select New
Window. Type in the following exactly as you see it there, except replace <your name
goes here with your name.
## SimpleAddition.py
## <your name goes
here
def main():
num1 = 7
num2 = 5
answer = num1 + num2
print "Sum =", answer
main()
b. Once you have typed the complete program, press "Control-S"
and save the program as SimpleArithmetic.py. Run it
by pressing F5. If you see an error message, you typed something incorrectly.
Compare the printout above with your program and fix any discrepancies found.
If you cannot find the error, ask the instructor for assistance. (Computers are
very picky about how programmers express things, and do not tolerate typos.)
Continue this process until the program runs successfully.
Congratulations!
You’ve successfully entered and executed your first complete Python program.
Demonstrate for
instructor.
5.
Writing your own programs.
Write a program
similar to the one above that subtracts two numbers and prints the
difference. Call this program SimpleDifference.py.
Demonstrate for
instructor.
6.
Writing your own program.
Write a program
to calculate the volume of a rectangular solid.
The user of the program will provide the length, width and height of the
box. Call this program BoxArea.py.
Demonstrate for
instructor.
7. Upload the files to
WebCT under the Assignments section:
Check list:
First.py
_______ SimpleAddition.py
_______
SimpleDifference.py _______ BoxArea.py _______
You must press
the SUBMIT button or the uploaded
programs will not be submitted to the professor’s account.
Remove from the
desktop any files and folders you created (that should include the First.py that
you downloaded – mail them to yourself if you wish to have a copy.) Then, empty
your recycle bin. Log out of your Cougars account.
7.
Log off of the computer you are using.