Sue Evans & Patti Ordóñez
Hit the space bar for next slide
Today's lab introduces you to emacs, the preferred text editor for this class, as well as the process of writing, debugging, and running your code.
Keep an eye on your quota!! Each user is given a certain amount of disk space (100MB) on the GL network. If you exceed this disk space, we cannot send you email (i.e. project grades) and you can't create files (i.e. homework & project files) To check your quota, at the linux prompt, type:
linux3[16]% quota -v Volume Name Quota Used %Used Partition user.natalie2 100000 63551 64% 37% linux3[17]%
This will show how much disk space you are using. If the percentage used is
approaching 100%, you need to clean up your account. This can be done by
typing oitcleaner
at the linux prompt:
linux3[18]% oitcleaner This is the cleaner that we want Volume Name Quota Used %Used Partition user.natalie2 100000 63551 64% 37% **** Start Quota **** **** Start: Cleaning Internet Files **** find: /afs/umbc.edu/users/n/a/natalie2/home/.2kprofile/Application Data/Mozilla/Profiles: No such file or directory **** Done: Cleaning Internet Files **** **** Start: Cleaning Misc. Files **** **** Done: Cleaning Misc. Files **** **** Finding Large Files **** rm: remove regular file `/afs/umbc.edu/users/n/a/natalie2/home/../pub/www/DMP/finalpresentation.ppt'? n rm: remove regular file `/afs/umbc.edu/users/n/a/natalie2/home/../pub/www/DMP/finalpaper.pdf'? n rm: remove regular file `/afs/umbc.edu/users/n/a/natalie2/home/../pub/www/DMP.tar'? n **** Finished **** Volume Name Quota Used %Used Partition user.natalie2 100000 63538 64% 37% **** End Quota **** linux3[19]%
This script will clear out some unnecessary files in your account. It will also ask you if you want to delete any large files it finds. You must answer either y or n when it prompts you to remove.
Python programs are written using a text editor. They are then executed independently of the editor. For this class, we'll be covering emacs only.
The version of emacs on the school machines have
many enhancements specifically for X Windows. There is a graphical
toolbar (like in MS Word etc..) for basic options as well as a text driven
toolbar for other options. To invoke emacs just type emacs, the name
of the file you want to edit, and if you do not want to tie up the shell,
use an & afterwards
emacs examplefilename.py &
The
shell is a program that is a command line interface to your operating
system when you open up a terminal window. The & starts up your program
in a new shell in a new terminal window. This allows you to edit in one
window and run your program in another instead of repeatedly getting into
and out of the editor. When working from home, the & doesn't work, so
just login twice to have two windows.
Some helpful Emacs commands
Emacs also has a learn by doing tutorial. You can find it by typing while Emacs is opened,
Since most of you will be using emacs as your text editor, we can make your life a little easier by providing a customized version of the .emacs file it uses. This file will cause you to have automatic indentation and syntax highlighting. This hidden file (the filename starts with a .) must be placed in your home directory in order for it to work properly.
In order to see hidden files, the ls comand has to be issued with a special flag, the -a flag.
You should have made a directory for all of your 201 homeworks and projects in Lab #1. If you didn't, at the linux prompt type mkdir 201 and make sure you do Lab 1 before you start doing homeworks.
Now let's create a directory for our labs.
cd into your 201 directory.
Type mkdir labs at the linux prompt.
Change into your labs directory and make a directory for today's lab,
lab #2 by typing cd labs, then mkdir lab2.
Change into your lab2 directory (cd lab2) and proceed to the next
step.
Now you are ready to start up emacs and begin typing your code.
If we are short on time, just type a very minimal comment. You'll see that the indentation will be done automatically. This is because the emacs text editor knows the format of Python files. Any file you make that ends in .py will be treated as a Python source file by the editor.
# File: convert.py # Written by: Patti Ordonez # Date: 5/?/09 # Lab Section: 03 # UMBC email: ordopa1@umbc.edu # Description: This program converts degrees in celsius to # degrees in fahrenheit. degC = input("What is the Celsius temperature ? ") degF = (9.0 / 5.0) * degC + 32 print "The temperature is ", degF, " degrees in fahrenheit"
There are actually a couple of ways you can run your program. We're going to try them.
>>> degC = 0
>>> degF = (9.0 / 5.0) * degC + 32
>>> print degF
# File: convert.py # Written by: Patti Ordonez # Date: 5/?/09 # Lab Section: 03 # UMBC email: ordopa1@umbc.edu # Description: This program converts degrees in celsius to # degrees in fahrenheit. def main(): degC = input("What is the Celsius temperature ? ") degF = (9.0 / 5.0) * degC + 32 print "The temperature is ", degF, " degrees in fahrenheit" main()
Download the following file into a file named errors.py by typing the command:
cp /afs/umbc.edu/users/s/l/slupoli/pub/labCode201/errors.py .
linux2[52]% cat errors.py # File: errors.py # Written by: Sue Evans # Date: 2/10/11 # Section: All # Email: bogar@cs.umbc.edu # Description: This program contains some buggy code for you to correct. def main(): print "This program finds the average of two integers." num1, num2 = input("Enter two numbers separated by a comma") average = (num1 + Num2) / 2 print "The average of the two numbers is " + average main()
Run the program using the command below.
Enter the values 3,4 when prompted.
It should produce the following error messages.
linux2[63]% python errors.py This program finds the average of two integers. Enter two numbers separated by a comma3,4 Traceback (most recent call last): File "errors.py", line 17, in ? main() File "errors.py", line 13, in main average = (num1 + Num2) / 2 NameError: global name 'Num2' is not defined
Examining the error messages after we entered the values 3,4, the interpreter is tracing back an error that occurred in line 17 to one that occurred in line 13 in main. It displays the line and tells us, we have a NameError. Num2 is not defined. Examine the code and try to fix the error.
linux2[63]% python errors.py This program finds the average of two integers. Enter two numbers separated by a comma3,4 Traceback (most recent call last): File "errors.py", line 17, in ? main() File "errors.py", line 13, in main average = (num1 + Num2) / 2 NameError: global name 'Num2' is not defined
Examining the error messages after we entered the values 3,4, the interpreter is tracing back an error that occurred in line 17 to one that occurred in line 15 in main. It displays the line and tells us that we have a TypeError. We can not concatenate 'str' objects which are of type string with 'int' objects which are of type integer. Examine the code at line 15 and try to fix the error.
linux2[85]% python errors.py This program finds the average of two integers. Enter two numbers separated by a comma3,4 Traceback (most recent call last): File "errors.py", line 17, in ? main() File "errors.py", line 15, in main print "The average of the two numbers is " + average TypeError: cannot concatenate 'str' and 'int' objects linux2[86]%
We are not getting any errors when we run the code, but the output is not correct. The average of the numbers 3 and 4 should be 3.5 not 3. Examine the code and try to fix the error.
linux2[97]% python errors.py This program finds the average of two integers. Enter two numbers separated by a comma3,4 The average of the two numbers is 3 linux2[98]%
Now the code is running exactly as expected. However, we would like to see a space after the word comma. Examine the code and try to fix the error.
linux2[104]% python errors.py This program finds the average of two integers. Enter two numbers separated by a comma3,4 The average of the two numbers is 3.5 linux2[105]%
This is ONLY for the the CMSC 201 staff!! This is not for a grade. We want to test the submission method. Back in the command prompt, type:
submit cs201 Test errors.py
Please inform your TA if it did not work. You only need to give them your USERNAME.
Congratulations, you have successfully debugged a Python script. If you have some time left over, attempt to debug the following code, which includes pieces of code we've yet to cover.
import math def isPrime(n): if n == 2: return True for divisor in range(3, int(math.sqrt(n)), 2): if n % divisor == 0: return False return True def main(): for i in range(2, 18): print "Is %d prime? %s" % (i, isPrime(i)) main()