Difference between revisions of "CSC111 Lab 2 2011"

From dftwiki3
Jump to: navigation, search
(Created page with "__TOC__ <bluebox> 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 neighb...")
 
(Transferring Python programs from Windows/Macs to your 111a-xx account)
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
--[[User:Thiebaut|D. Thiebaut]] 14:45, 14 September 2011 (EDT)
 +
----
 +
 
__TOC__
 
__TOC__
  
Line 5: Line 8:
 
</bluebox>
 
</bluebox>
  
==Login/logout==
+
=Login/logout=
  
To '''login''' refers to the action of connecting to the computer as a user. To connect to Beowulf, the computer 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.
+
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 on the desktop (or via the Start/Program menu) and click on it.
 
  
If the Quick Connect window is not greeting you, click on the Quick Connect icon to get it:
+
==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.
 +
<br />
 +
<center>
 +
[[Image:putty1.png|500px]]
 +
</center>
 +
<br />
 +
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 as the host name,
+
*Enter '''beowulf.csc.smith.edu''' or '''emmy.smith.edu''' as the host name, and press '''Enter'''
111a-xx as your user name (change the xx to reflect your 2-letter Id)
+
* When prompted to '''login as''', use your '''111a-xx''' account as your user name (change the xx to reflect your 2-letter Id)  
Click on the Connect button
+
* Enter your password.
Enter your password.
+
* Press '''Enter'''
and click "Connect".
 
  
If everything went well, you should see this:
+
If everything goes well, you should see this:
  
 
  SSH Secure Shell 3.1.0 (Build 235)
 
  SSH Secure Shell 3.1.0 (Build 235)
Line 30: Line 38:
 
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.
 
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 times. If you are working with a lab partner, it is a good time to log out and let her/him try the procedure.
+
==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.
  
To logout of the session with beowulf, simply type '''exit''' at the Linux prompt:
+
* Enter your password when prompted.
  
  [111a-xx@beowulf ~]$  '''exit'''
+
==Windows &amp; Mac==
  
Don't hesitate to call on the TA or your instructor if you need help.
+
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.
  
==Playing with the Emacs Editor==
+
To logout of the session with beowulf or emmy, simply type '''exit''' at the Linux prompt:
  
In order for you not to suffer from a misbehaving backspace/delete key as happened in class yesterday, you should execute the following command (your input is in bold face)
+
  [111a-xx@beowulf ~]$  '''exit'''
  
  [111a-xx@beowulf ~]$  '''getcopy .emacs'''
+
Don't hesitate to call on the TA or your instructor if you need help.
  cp: overwrite `./.emacs'? '''y'''
 
  
(That is "getcopy" space period "emacs")
+
=Playing with the Emacs Editor=
It's a configuration file that will allow emacs to understand the backspace key correctly. ''You only need to do this once and you will be set for the semester.''
 
  
 
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 [http://www.robertfulghum.com/ Robert Fulghum's] book [http://en.wikipedia.org/wiki/All_I_Really_Need_to_Know_I_Learned_in_Kindergarten ''All I really need to know I learned in Kindergarten''].
 
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 [http://www.robertfulghum.com/ Robert Fulghum's] book [http://en.wikipedia.org/wiki/All_I_Really_Need_to_Know_I_Learned_in_Kindergarten ''All I really need to know I learned in Kindergarten''].
Line 115: Line 133:
 
</tanbox>
 
</tanbox>
  
Part I of the homework assignment for this week 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.
 
 
 
 
==Paying with Python in Interactive Mode==
 
 
 
* At the Linux prompt, type:
 
 
 
  [111a-xx@beowulf ~]$  '''python'''
 
 
 
followed by the '''Return''' key.  Try some of the examples we did in class yesterday.  See if you can guess how python will interpret the commands before you press the '''Return''' key.
 
 
 
<code><pre>
 
Python 2.6 (r26:66714, Nov  3 2009, 17:33:38)
 
[GCC 4.4.1 20090725 (Red Hat 4.4.1-2)] on linux2
 
Type "help", "copyright", "credits" or "license" for more information.
 
 
>>> 3
 
 
 
>>> 4
 
 
 
>>> 6
 
 
>>> 3 + 4
 
 
>>> a = 3
 
 
>>> b = 4
 
 
>>> a
 
 
>>> a + b
 
 
>>> a * 2 + b
 
 
>>> 'first' = 'Carole'
 
                                                (why does this generate an erro?)
 
                                     
 
>>> first = 'Carole'
 
 
 
>>> last = 'Christ'
 
 
 
>>> first
 
 
 
>>> first + last
 
 
 
>>> first * 2
 
 
 
>>> ( first + ' ' ) * 2                  (predict the output before you press Return)
 
 
 
>>> fullname = first + ' ' + last
 
 
 
>>> print fullname
 
 
 
>>> print 'fullname'                  (predict the output before you press Return)
 
 
 
                                              (does the output make sense?)
 
>>>
 
  
</pre></code>
+
=Creating a Python program with Emacs=
  
* Type '''Control-D''' to exit the Python interactive shell and to return to the Linux prompt.
+
* 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.
 
 
 
 
==Creating your first Python program==
 
 
 
* 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.
 
  
 
* 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 194: Line 151:
 
* Type in (or copy-paste) the following lines
 
* Type in (or copy-paste) the following lines
  
 +
<br /><br />
 
<source lang="python">
 
<source lang="python">
  
Line 200: Line 158:
 
# 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 206: Line 163:
 
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 )
  
 
main()
 
main()
  
 
</source>
 
</source>
 
+
<br /><br />
* 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 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:
 
* 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'''
  
  
Line 225: Line 183:
  
 
   File "temp.py", line 5
 
   File "temp.py", line 5
     print "The temperature in Celcius is"  tempC
+
     print( "The temperature in Celcius is"  tempC )
 
                                         ^
 
                                         ^
 
  SyntaxError: invalid syntax
 
  SyntaxError: invalid syntax
Line 231: Line 189:
 
:then you probably didn't type the program correctly. Make sure didn't forget double quotes or commas, and spelled all the words correctly.
 
: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.
+
* 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==
+
=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'''
 
* If you are still inside emacs, return to the Linux prompt by typing '''C-x C-c'''
Line 247: Line 207:
  
 
* And type in (or copy/paste) the program below:
 
* And type in (or copy/paste) the program below:
 +
<br />
 +
<br />
  
 
<source lang="python">
 
<source lang="python">
Line 254: Line 216:
  
 
def main():
 
def main():
     first = raw_input( "Enter your first name, please: " )
+
     first = input( "Enter your first name, please: " )
     last  = raw_input( "Enter your last name, please:  " )
+
     last  = input( "Enter your last name, please:  " )
     print "Welcome to CSC111,", first, last, "!"
+
     print( "Welcome to CSC111,", first, last, "!" )
  
 
main()
 
main()
  
 
</source>
 
</source>
 +
<br />
 +
<br />
  
 
* Save your program and return to the Linux prompt with '''C-x C-c"
 
* Save your program and return to the Linux prompt with '''C-x C-c"
Line 266: Line 230:
 
* Run your program
 
* Run your program
 
    
 
    
     [111a-bz@beowulf ~]$  '''python getname.py'''
+
     [111a-bz@beowulf ~]$  '''python3 getname.py'''
  
 
* Verify that it runs without errors.
 
* Verify that it runs without errors.
 +
<br />
 +
<br />
  
* Once it works correctly, modify your program again, and make it ask for  
+
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
 
 +
==Challenge #1==
 +
|}
 +
[[Image:QuestionMark1.jpg|right|120px]]
 +
* Once your program works correctly, modify it again, and make it ask for  
 
** the town where you were born
 
** the town where you were born
 
** the country where you were born
 
** the country where you were born
 
:and make it display this information in the following fashion:
 
:and make it display this information in the following fashion:
  
     [111a-bz@beowulf ~]$  '''python getname.py'''
+
     [111a-bz@beowulf ~]$  '''python3 getname.py'''
 
     Enter your first name, please: '''Alice'''
 
     Enter your first name, please: '''Alice'''
 
     Enter your last name, please: '''Doe'''
 
     Enter your last name, please: '''Doe'''
Line 285: Line 258:
 
* When your program works, move on to the next section.
 
* When your program works, move on to the next section.
  
==Version 2 of your temperature program program==
+
=Version 2 of your temperature program=
  
 
* Make sure you are back at the Linux prompt
 
* Make sure you are back at the Linux prompt
Line 297: Line 270:
 
Here's an example of the way the program should work (the user input is in bold face):
 
Here's an example of the way the program should work (the user input is in bold face):
  
   [111a-xx@beowulf ~]$  '''python temp.py'''
+
   [111a-xx@beowulf ~]$  '''python3 temp.py'''
 
   
 
   
 
  What town to you live in? '''Northampton'''  
 
  What town to you live in? '''Northampton'''  
Line 307: Line 280:
  
 
<bluebox>
 
<bluebox>
If you get stuck, don't hesitate to ask for help!
+
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! :-)
 
</bluebox>
 
</bluebox>
  
Line 313: Line 286:
 
Make sure your program works and outputs the correct output before moving on to the next part.
 
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.   
+
=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:
 +
 
 +
<br /><br />
 +
<br />
 +
 
 +
<source lang="python">
 +
# 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()
 +
 
 +
</source>
 +
<br />
 +
<br />
 +
<br />
 +
 
 +
* Save your program and run it.
 +
* Verify that it runs without errors.
 +
<br />
 +
<br />
 +
 
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
 
 +
==Challenge #2==
 +
|}
 +
[[Image:QuestionMark2.jpg|right|120px]]
 +
* 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'''.
 +
<br />
 +
<br />
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
 
 +
==Challenge #3==
 +
|}
 +
[[Image:QuestionMark3.jpg|right|120px]]
 +
* 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:
 +
 
 +
<br />
 +
<br />
 +
<code><pre>
 +
[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
 +
 
 +
</pre></code>
 +
<br />
 +
<br />
 +
 
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
 
 +
==Challenge #4==
 +
|}
 +
[[Image:QuestionMark4.jpg|right|120px]]
 +
* Modify your program again and make state the name of the city when it asks for the state:
 +
<br />
 +
<br />
 +
<br />
 +
 
 +
<code><pre>
 +
[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
 +
 
 +
</pre></code>
 +
 
 +
<br />
 +
<br />
 +
 
 +
=More about the '''print''' function=
  
There is nothing to hand back for this lab. 
+
* Try this new program:
  
If you have time and want to check the homework assignment, feel free to have a look at it!
+
<br />
 +
<br />
 +
<source lang="python">
 +
def main():
 +
    list = [ "one", "two", "three", "four", "five" ]
 +
    for item in list:
 +
        print( item, end='|' )
 +
    print()
  
  
==Homework #1==
+
main()
 +
 
 +
</source>
 +
<br />
 +
<br />
 +
* 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.
  
You are now ready for [[CSC111 Homework 1 | Homework #1]]!
+
<br />
 +
<br />
  
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.
+
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Challenge #5==
 +
|}
 +
[[Image:QuestionMark5.jpg|right|120px]]
 +
* Modify your temperature program so that its output looks something like this:
  
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.
+
 +
 +
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,
 +
  
==How to Installing and using Secure Shell on a Windows PC or a Mac==
+
<br />
 +
<br />
 +
 
 +
=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 [http://www.smith.edu/its/tara/file_transfer/ssh_sftp.html Web site] for more information.
 
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 [http://www.smith.edu/its/tara/file_transfer/ssh_sftp.html Web site] for more information.
  
[[Category:CSC111]][[Category:Labs]]
+
=Transferring Python programs=
 +
 
 +
* 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!!!
 +
<br />
 +
 
 +
=Done!=
 +
* That's it for this lab!
 +
* If you still have time before the 2-hour lab period is over, start working on [[CSC111_HW_1_2011 |Homework #1]].
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
 
 +
[[Category:CSC111]][[Category:Labs]][[Category:Python]]

Latest revision as of 22:28, 11 February 2014

--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,



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

  • 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.