CSC111 Lab 3

From dftwiki3
Revision as of 14:21, 8 February 2010 by Thiebaut (talk | contribs) (Created page with '=Software Setup= Before you can use the JES programming environment, you need to install it first. * Login to your Novell account. * Open your H: drive in Windows Explorer. …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Software Setup

Before you can use the JES programming environment, you need to install it first.

  • Login to your Novell account.
  • Open your H: drive in Windows Explorer.
  • Open Windows Explorer a second time, so that you have two windows open.
  • In the second window, enter this address in the Address bar: \\rescent\pc\COURSES\CSC\CSC111
This is the place on the Novell network where you will find many files related to the homework assignments and the labs.
  • Drag the folder named jes-3-1-1windows from the CSC111 window to your H: drive.
    JESicon.png
  • Close the windows showing the CSC111 folder
  • In the window showing your H: drive, open the JES folder and click on the JES icon (snake face with glasses :-)
  • You should get the JES programming environment.




JESidePC.png


If you want to install JES on your personal computer

If you want to install JES on your Mac or Windows laptop or computer, you can get also get JES from the Web, at this address http://coweb.cc.gatech.edu/mediaComp-teach/26. Click on the the file jes-3-1-1-windows.zip if you use Windows, or on jes-3-1-1-macosx.dmg if you have a Mac, and install as you would a regular application.

Notes for Mac users

If you want to connect to the drop box for the class from a Mac, use the Finder Menu, Go, Connect to Server, enter rescent.smith.edu as the server, then click on COURSES, CSC, and finally CSC111. You're there!

Lab Exercises

Open JES by double-clicking on the icon or navigating through the start menu (on pcs)

  • In the window that opens, the top portion is program area, and the bottom portion is command area.
  • You can enter Python commands directly into the command area. Note that they will be lost when you close JES.
  • You can create functions and programs (also called recipes, in the text) to execute your commands, in the program area and save them with a filename ending in ‘.py’. You then can load these programs into the command area using the ‘Load’ button in the center-left of the JES window, and ‘call’ the functions to execute the commands you typed in and saved. You call a function (or recipe) by typing its name, with parentheses ‘()’ in the command window. We will do this below.


In the following exercises, you should type all the lines beginning with ‘>’ in the JES command window. Try to anticipate what response Python will give you once you type each command and press enter.

Command Area Exercises

Begin your lab exercises in the command area (the bottom part of the JES window). Type the following commands in this command area, and see what response you get from the Python interpreter.

> 2 + 3 
> print  2 + 3

Now you will define a variable named ‘sum’ and then ask Python to display the value assigned to ‘sum’ by using a print statement

> sum = 2 + 3
> print sum

Below you will define a ‘character string’ (a list of characters), and then print out a statement using both the string “sum” and variable named sum. You will use print statements like the second one below very often as you learn to write more complicated programs and need to ‘debug’ them (i.e., find errors in what you have written)

> print "sum"
> print "sum = ", sum

You can multiply both numbers and strings! Remember that to a computer everything is a symbol, and is ultimately reduced to 1s and 0s.

> print 4 * 5
> print sum * 5  
> print "sum" * 5

Data types are a fundamental concept in computer programming, and something you use implicitly all the time when performing math on your own. For computer programming, you must always be very clear on what data type you are using. Above you used two different data types – integer and character string. Now you will use real numbers and see the difference in the result of a simple mathematical operation when you specify that Python must use integers versus real (or floating point) numbers. Do you get an unexpected result from either of these commands? Can you think of why this happened (this is discussed in the text)?

> print 2/3
> print 2.0/3.0

Note that only one of the numbers in your expression needs to be specified as a real number for Python to perform the operation as a floating point operation

> print 2.0/3

Finally, play with strings a little. In which of the examples below is the Python output of the print statement the same as the first “hello world” ?

> print "hello world"
> print "hello", "world" 
> print "hello" + "world"
> print "hello" + " world"


EXERCISE #1
Assume that you have defined a variable called dash as follows:

> dash = '-'

Find a way to use dash to print a line of 60 dashes, such as this one:

------------------------------------------------------------


EXERCISE #2
Assume now that you have defined two variables, dash and circle as follows:

> dash = '-'
> circle = 'o'

Find a way to use both variables and print a line of 60 characters, such as this one:

-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o


EXERCISE #3
Once you have a solution for Exercise 2, create a new variable called line in which you will store the recipe generating the line of 60 dashes.

> line = ... (copy your recipe for generating 60 dashes here)

and print 5 lines of 60 dashes using only the new variable line that you will have just created.

Program Area Exercises

In the Program area, type in the following lines, being careful to follow the format exactly (including all punctuation and indenting)

def hello():
  print "hello world"
def hello2(name):
  print "hello", name

Next, save these lines you have typed in a file named hello.py. You should save the file in a folder on your H: drive. Then load these commands into the command area by clicking on the ‘Load’ button in the center left of the JES window.

To see how your functions, or recipes, work, type the following lines in the control area. Note that some of these commands will give you error messages, rather than what you might expect as output from the recipes. What is different between the commands the produce the expected output and those that produce error messages?

> hello
> hello()
> hello2("Lucy")
> hello2(Lucy)
> sum = 2 + 3
> hello2("sum")
> hello2(sum)

EXERCISE #4
Create a function (a recipe) to print a line of 60 dashes. You should use the solution you found earlier in Exercise 1. Your function should look something like this:

def lineOfDashes():

dash = "-"
print ... (put your recipe from Exercise 1 here)



Make sure you click on the load button to save your function.

Getting Ready for the Homework

To be sure you will be able to submit your homework, navigate to the homework folder now. This folder is a ‘dropbox’ on one of the Smith College computers. You can navigate to it as you did for installing JES at the start of the lab, or through the following path:

  • My Network Place\Entire Network\NetWare Services\rescent\pc\COURSES\CSC\CSC111\Dropbox