Difference between revisions of "CSC111 Lab 2 2011"

From dftwiki3
Jump to: navigation, search
(Challenge #3)
(Challenge #4)
Line 370: Line 370:
  
 
<code><pre>
 
<code><pre>
  [111a-bz@beowulf ~]$ '''python3 temp.py'''
+
  [111a-bz@beowulf ~]$ python3 temp.py
  
 
  What city were the temperatures recorded in? Northampton
 
  What city were the temperatures recorded in? Northampton
Line 388: Line 388:
 
<br />
 
<br />
 
<br />
 
<br />
 
 
  
 
=More about the '''print''' function=
 
=More about the '''print''' function=

Revision as of 15:23, 15 September 2011

--D. Thiebaut 14:45, 14 September 2011 (EDT)


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.

Windows

Using the Windows PC in front of you, find the Putty program/icon in the list of applications or programs, and click on it.

Putty1.png


In the Hostname bar, type in beowulf.csc.smith.edu or emmy.smith.edu. 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.smith.edu or emmy.smith.edu as the host name, and press Enter
  • When prompted to login as, use your 111a-xx account as your user name (change the xx to reflect your 2-letter Id)
  • Enter your password.
  • Press Enter

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.

Mac

  • Find the Terminal application and start it.
  • At the prompt in the terminal, type
   ssh -Y   111a-xx@beowulf.csc.smith.edu


or
  ssh -Y 111a-xx@emmy.smith.edu
and make sure you replace xx by your two-letter Id.
  • Enter your password when prompted.

Windows & Mac

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 four 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 Fahrenheit.
  • Replace the division symbol / in your program by this symbol: //. Run your program. See the difference? Can you tell what operation the // symbol performs?

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 = input( "Enter your first name, please: " )
    last  = 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 ~]$  python3 getname.py
  • Verify that it runs without errors.



Challenge #1

QuestionMark1.jpg
  • Once your program works correctly, modify it 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 ~]$  python3 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

  • 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 ~]$  python3 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, try different ideas. Play with the code. Stay within your logic. Don't try random statements. If you really get stuck, then ask for help!  :-)


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

Converting several temperatures to Celcius

  • Modify your temp.py program or create a new one in a different file (with a .py extension), and enter the following code in it:




# temp2.py
# yourname 111a-xx
#

def main():
   temps = [ 32, 100, 60, 70, 75, 80, 90 ]
   for tempF in temps:
        print( "temperature in degree F =", tempF )

main()




  • Save your program and run it.
  • Verify that it runs without errors.



Challenge #2

QuestionMark2.jpg
  • Modify your program and make it print both the temperature in Fahrenheit and Celsius for each number in the list.
  • Make your program output the values in Fahrenheit and Celsius on the same line.



Challenge #3

QuestionMark3.jpg
  • Modify your program again and make it ask the user first for the name of the city and the name of the state where the temperatures were recorded. Then the program will output something like this:



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

 What city were the temperatures recorded in? Northampton
 What state is this city in? MA


 The following temperatures were measured in Northampton, MA:
 -  32 F or  0 C
 -  100 F or  37 C
 -  60 F or  15 C
 -  70 F or  21 C
 -  75 F or  23 C
 -  80 F or  26 C
 -  90 F or  32 C



Challenge #4

QuestionMark4.jpg
  • Modify your program again and make state the name of the city when it asks for the state:




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

 What city were the temperatures recorded in? Northampton
 What state is Northampton in? MA

 The following temperatures were measured in Northampton, MA:
 -  32 F or  0 C
 -  100 F or  37 C
 -  60 F or  15 C
 -  70 F or  21 C
 -  75 F or  23 C
 -  80 F or  26 C
 -  90 F or  32 C



More about the print function

  • Try this new program:



def main():
    list = [ "one", "two", "three", "four", "five" ]
    for item in list:
        print( item, end='|' )
    print()


main()



  • Run it.
  • See what the end='|' argument does for the print function?
  • Replace end='|' and instead type end='\n' (remember that \n means end-of-line for Python)
  • See the new output?
  • Once more: replace end='\n' and instead write end=', ' (that's comma followed by space inside quotes).
  • See the different output? Does that make sense?
  • Try different strings of characters between the quotes associated with end=. This is a nice way to control the way the print functions operates.



Challenge #5

QuestionMark5.jpg
  • Modify your temperature program so that its output looks something like this:


The temperatures in Fahrenheit in Northampton are: 32, 100, 60, 70, 75, 80, 90,
The equivalent temperatures in Celsius are: 0, 37, 15, 21, 23, 26, 32,



Information about 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.

Transferring Python programs from Windows/Macs to your 111a-xx account

  • For right now the best way is to use copy/paste to take the code from Idle on your Mac or PC, and paste it into an ssh window where you will be in edit mode with emacs.
  • You may have to check that the pasting of the text didn't change the indentation of your code.
  • Very Important: Once your program is pasted, save it, and run it on the Linux machine to make sure that it works the same way as it does on your Mac or Windows PC!!!


Done!

  • That's it for this lab!
  • If you still have time before the 2-hour lab period is over, start working on Homework #1.