Difference between revisions of "CSC111 Lab 3"

From dftwiki3
Jump to: navigation, search
(Notes for Mac users)
(Lab Exercises)
Line 39: Line 39:
  
 
Open JES by double-clicking on the icon or navigating through the start menu (on pcs)
 
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.
+
* In the window that opens, the top portion is program area, and the bottom portion is command area (the interactive window we have already seen on beowulf).
* 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.
 
  
 
+
==Creating and running programs==
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"
 
 
 
<br />
 
{|  style="width:100%;"
 
| style="background:khaki; color:darkblue; width:100%;"  |
 
'''EXERCISE #1''' <br />
 
Assume that you have defined a variable called '''dash''' as follows:
 
: <tt>> dash = '-'</tt>
 
Find a way to use dash to print a line of 60 dashes, such as this one:
 
:------------------------------------------------------------
 
|}
 
 
 
<br />
 
{|  style="width:100%;"
 
| style="background:khaki; color:darkblue; width:100%;"  |
 
'''EXERCISE #2''' <br />
 
Assume now that you have defined two variables, '''dash''' and '''circle''' as follows:
 
: <tt> > dash = '-'</tt>
 
: <tt> > circle = 'o'</tt>
 
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
 
|}
 
 
 
<br />
 
{|  style="width:100%;"
 
| style="background:khaki; color:darkblue; width:100%;"  |
 
'''EXERCISE #3''' <br />
 
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.
 
: <tt> > line =  ... ''(copy your recipe for generating 60 dashes here)'' </tt>
 
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)
 
In the Program area, type in the following lines, being careful to follow the format exactly (including all punctuation and indenting)
  
def hello():
+
<source lang="python">
  print "hello world"
+
def main():
 
+
   name = raw_input( "what is your name? " )
def hello2(name):
+
  print "hello there, " + name + "!"
   print "hello", name
+
</source>
 
 
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)
 
  
{| style="width:100%;"
+
* Save the file by typing Control-S.
| style="background:khaki; color:darkblue; width:100%;"  |
+
* A popup window opens up. Save the file as '''hello.py''' in your '''H:''' drive
'''EXERCISE #4''' <br />
+
* Click on '''Load Program''' button to load the file just stored in JES.
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:
+
* In the command area (black window), type
<br />
 
<br />
 
<tt> def lineOfDashes(): </tt>
 
:<tt>dash = "-" </tt>
 
:<tt>print ... </tt>    ''(put your recipe from Exercise 1 here)''
 
  
<br />
+
  main()
<br />
 
Make sure  you click on the ''load'' button to save your function.
 
|}
 
  
==Getting Ready for the Homework==
+
:to call the function.
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:
+
* Make sure you see the pop-up window asking for your name, and the response of the program in the interactive area.
* My Network Place\Entire Network\NetWare Services\rescent\pc\COURSES\CSC\CSC111\Dropbox
 

Revision as of 14:55, 8 February 2010

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 :-)
  • After a few (long) seconds, 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

Jes for the Mac is available on Google code. Get Jes 3.2.1 for the Mac and install on your mac.


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 (the interactive window we have already seen on beowulf).

Creating and running programs

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

def main():
   name = raw_input( "what is your name? " )
   print "hello there, " + name + "!"
  • Save the file by typing Control-S.
  • A popup window opens up. Save the file as hello.py in your H: drive
  • Click on Load Program button to load the file just stored in JES.
  • In the command area (black window), type
  main()
to call the function.
  • Make sure you see the pop-up window asking for your name, and the response of the program in the interactive area.