Difference between revisions of "CSC111 Lab 2 2011"
(→Creating a Python program with Emacs) |
|||
Line 147: | Line 147: | ||
</source> | </source> | ||
<br /><br /> | <br /><br /> | ||
− | * Be careful that the | + | * 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: | ||
Line 168: | Line 168: | ||
* Replace the division symbol '''/''' in your program by this symbol: '''//'''. Run your program. See the difference? Can you tell what operation the '''//''' symbol performs? | * 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= |
Revision as of 14:23, 14 September 2011
Contents
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.
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 |
- 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 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, don't hesitate to 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
Challenge #2 |
- 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 |
- 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
|}
- 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
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.