Difference between revisions of "CSC111 Lab 2 2011"

From dftwiki3
Jump to: navigation, search
(Paying with Python in Interactive Mode)
(Creating your first Python program)
Line 111: Line 111:
 
Part I of the second homework assignment (which will start next Tuesday)  is to edit the complete story about Larry Walters and his flight over Los Angeles.
 
Part I of the second homework assignment (which will start next Tuesday)  is to edit the complete story about Larry Walters and his flight over Los Angeles.
  
==Creating your first Python program==
+
==Creating a Python program with Emacs==
  
* You are now ready to create your first Python program. Don't worry if what you are doing does not make sense yet. We will go over all of these steps in details in class these coming weeks.
+
* You are now ready to create Python program for the first time with emacs. Don't worry if what you are doing does not make sense yet. We will go over all of these steps in details in class these coming weeks.
  
 
* Make sure you have exited emacs and are back to the Unix prompt.
 
* Make sure you have exited emacs and are back to the Unix prompt.
Line 133: Line 133:
 
# yourfirstname yourlastname
 
# yourfirstname yourlastname
 
#
 
#
# My first python program                             
 
 
# Gets a temperature in Farenheit and prints its equivalent
 
# Gets a temperature in Farenheit and prints its equivalent
 
# in Celsius
 
# in Celsius
Line 139: Line 138:
 
def main():
 
def main():
 
       tempF = input( "Enter temperature in Farenheit: " )
 
       tempF = input( "Enter temperature in Farenheit: " )
 +
      tempF = eval( tempF )
 
       tempC = ( tempF - 32 ) * 5 / 9
 
       tempC = ( tempF - 32 ) * 5 / 9
 
       print "The temperature in Celcius is", tempC
 
       print "The temperature in Celcius is", tempC
Line 146: Line 146:
 
</source>
 
</source>
  
* Be careful that the three lines under '''def main():''' are indented exactly the same way.  It's an important aspect of python, that the indentation indicates a form of hierarchy of the program.
+
* Be careful that the three lines under '''def main():''' are indented exactly the same way.  It's an important aspect of python, that the indentation indicates a form of ''hierarchy'' controlling the statements.
  
 
* Type '''c-x c-s''' (control-x followed by control-s) to save the file. In Window 2, type in the following command at the Unix prompt to run the program:
 
* Type '''c-x c-s''' (control-x followed by control-s) to save the file. In Window 2, type in the following command at the Unix prompt to run the program:
  
  
   [111a-bz@beowulf ~]$ '''python temp.py'''
+
   [111a-bz@beowulf ~]$ '''python3 temp.py'''
  
  

Revision as of 13:59, 14 September 2011

Feel free to work in pairs, share insights and help your neighbors during the lab. This is true of all labs; the labs are a good time to cooperate with neighbors or work in pairs.

Login/logout

To login refers to the action of connecting to the computer as a user. To connect to Beowulf or Emmy, the computers used for Computer Science classes, one must have an account and a password. Logout is the term used when you are finished with the computer and want to terminate your session. You should have a Unix account form at this point, and have found on it your account name (of the form 111a-xx, where xx are two letters, different for everybody), and your password. Using the Windows PC in front of you, find the SecureShell program/icon in the list of applications or programs, and click on it.

If the Quick Connect window is not greeting you, click on the Quick Connect icon to get it.

You may use beowulf.csc or emmy.csc as the computers you are going to connect to. Pick a server different from the server used by your neighbor, and this way the two will be loaded approximately the same, and the response time will be better for everybody.

  • Enter beowulf.csc or emmy.csc as the host name,
  • 111a-xx as your user name (change the xx to reflect your 2-letter Id)
  • Click on the Connect button
  • Enter your password.
  • and click "Connect".

If everything goes well, you should see this:

SSH Secure Shell 3.1.0 (Build 235)
Copyright (c) 2000-2001 SSH Communications Security Corp - http://www.ssh.com/

This copy of SSH Secure Shell is a non-commercial version.
This version does not include PKI and PKCS #11 functionality.

[111a-xx@beowulf ~]$ 

Keep your account and password information in a safe place, or learn both by heart! You will need them every time you need to use your account and work on your homework assignment or lab.

You may want to log out now, to practice this login procedure a couple of times. If you are working with a lab partner, it is a good time to log out and let her/him try the procedure.

To logout of the session with beowulf or emmy, simply type exit at the Linux prompt:

 [111a-xx@beowulf ~]$  exit

Don't hesitate to call on the TA or your instructor if you need help.

Playing with the Emacs Editor

You will now get a copy of a file from your instructor's account, and edit it. The file is called Fulghum and is a short story from Robert Fulghum's book All I really need to know I learned in Kindergarten.

To get a copy of this file, type


  [111a-xx@beowulf ~]$  getcopy fulghum


followed by the return key. Then edit the file with the command


  [111a-xx@beowulf ~]$  emacs fulghum


Simply work on the first paragraph of the file, and try to straighten it up. I have indicated below, on the right-hand side, which emacs commands you can use to fix the text on your screen.

Fulghum emacs.jpg

When you are done with the modification, or if you want to stop for right now and save your modifications, type C-x C-c, and press y when emacs asks if you want to save the file. Below is a summary of some useful emacs commands.

You should find some time this week to go to a lab and read the Emacs at Smith handout while in front of the terminal, on your own. This is the best way to test and learn the different features of the editor!

DEL/Shift Backspace Delete previous character
c-d Delete character at cursor
Esc-d Delete word
c-k Kill line
c-a Go to beginning of line
c-e Go to end of line
c-f Move cursor 1 character forward
c-b Move cursor 1 character backward
c-p Move to previous line
c-n Move to next line
c-x c-c quit emacs and save file
c-x c-s save file but remain in emacs
c-g GE OUT OF TROUBLE!

Part I of the second homework assignment (which will start next Tuesday) is to edit the complete story about Larry Walters and his flight over Los Angeles.

Creating a Python program with Emacs

  • You are now ready to create a Python program for the first time with emacs. Don't worry if what you are doing does not make sense yet. We will go over all of these steps in details in class these coming weeks.
  • Make sure you have exited emacs and are back to the Unix prompt.
  • Open a second Secure Shell (ssh) window. You will use one to edit your program, the other one to execute your program.


  • In Window 1, use emacs to create a file that will contain your program:


  [111a-bz@beowulf ~]$ emacs temp.py
  • Type in (or copy-paste) the following lines
# temp.py
# 111a-xx (change and use your own account)
# yourfirstname yourlastname
#
# Gets a temperature in Farenheit and prints its equivalent
# in Celsius
 
def main():
      tempF = input( "Enter temperature in Farenheit: " )
      tempF = eval( tempF )
      tempC = ( tempF - 32 ) * 5 / 9
      print "The temperature in Celcius is", tempC

main()
  • Be careful that the three lines under def main(): are indented exactly the same way. It's an important aspect of python, that the indentation indicates a form of hierarchy controlling the statements.
  • Type c-x c-s (control-x followed by control-s) to save the file. In Window 2, type in the following command at the Unix prompt to run the program:


  [111a-bz@beowulf ~]$ python3 temp.py


  • If you get a message of this form:


 File "temp.py", line 5
   print "The temperature in Celcius is"  tempC
                                        ^
SyntaxError: invalid syntax
then you probably didn't type the program correctly. Make sure didn't forget double quotes or commas, and spelled all the words correctly.
  • You will know that your program works if you see it output the equivalent Celsius temperature of one expressed in Farenheit. A quick way to check that the program works correctly is to verify that the program outputs 0 for 32 degrees farenheit, and 5 degrees Celsius for 41 farenheit.

Another program: ask the user for her name

  • If you are still inside emacs, return to the Linux prompt by typing C-x C-c
  • Your cursor should be at the Linux prompt :


  [111a-xx@beowulf ~]$ _
  • You are going to create a new program. Let's call it getname.py
  [111a-bz@beowulf ~]$  emacs getname.py
  • And type in (or copy/paste) the program below:
# getname.py
# yourname here
# 111a-xx (replace xx by your two-letter Id)

def main():
    first = raw_input( "Enter your first name, please: " )
    last  = raw_input( "Enter your last name, please:  " )
    print "Welcome to CSC111,", first, last, "!"

main()
  • Save your program and return to the Linux prompt with C-x C-c"
  • Run your program
   [111a-bz@beowulf ~]$  python getname.py
  • Verify that it runs without errors.
  • Once it works correctly, modify your program again, and make it ask for
    • the town where you were born
    • the country where you were born
and make it display this information in the following fashion:
   [111a-bz@beowulf ~]$  python getname.py
   Enter your first name, please: Alice
   Enter your last name, please: Doe
   Which town were you born in? Northampton
   What country is this in?          USA 
  
   Alice Doe, of Northampton, USA, welcome to CSC111 !
  • When your program works, move on to the next section.

Version 2 of your temperature program program

  • Make sure you are back at the Linux prompt
  • Edit your program one more time:
 [111a-bz@beowulf ~]$ emacs temp.py


  • Modify your program so that it prompts the user for the name of the town first, then the name of the state, then for the temperature in Fahrenheit, and then outputs the town, the state, and the temperature in Celsius.

Here's an example of the way the program should work (the user input is in bold face):

 [111a-xx@beowulf ~]$  python temp.py

What town to you live in? Northampton 
What state is this in? Massachusetts
What is the current temperature in degrees Fahrenheit? 41

The temperature in Northampton in Massachusetts is 5 degrees Celsius.


If you get stuck, don't hesitate to ask for help!


Make sure your program works and outputs the correct output before moving on to the next part.

That's enough for the first lab. The purpose of the lab is more to get you acquainted with the programming environment than writing Python code.

There is nothing to hand back for this lab.

If you have time and want to check the homework assignment, feel free to have a look at it!


Homework #1

You are now ready for Homework #1!

You can also get it by going to your instructor's Web page, http://cs.smith.edu/~thiebaut, and by clicking on the CSC111 shortcut.

Locate the schedule page, then the week by week calendar, and the link to the first homework assignment. It is due the evening of Thursday Feb. 7th, at midnight.

How to Installing and using Secure Shell on a Windows PC or a Mac

The Smith College Technology Services Web pages have some good information on how to install the Secure Shell client on a Windows machine. Visit their Web site for more information.