2010 September 14 / jj|d|a|v|i|s|@|c|a|r|l|e|t|o|n|.|e|d|u
Carleton College CS 201, Fall 2010, Prof. Joshua R. Davis
This assignment is short and simple, I hope. It is due by noon on Wednesday.
We did not go over the syllabus in class, but you are responsible for knowing everything on it. For example, how do you activate your late pass? What task must you complete sometime during the first two weeks of class?
Go to a Carleton CS lab such as CMC 306 or CMC 304. Start a computer into Mac OS X (if you can't find one already running it) and log in. There are four applications that we'll use heavily. All four should be in your Dock; if not, then find them and put them there. Ask for help if you need it.
# 2010 August 29 by Joshua R. Davis import random def factorial(n): """The input n is a nonnegative integer. Returns the factorial n!.""" if n == 0: return 1 else: return n * factorial(n - 1) if __name__ == "__main__": for i in range(6): # Generate a somewhat random integer between 0 and 20 (more like 0 and 19). n = int(20.0 * random.random()) print factorial(n), "=", n, "!" # Launch the help viewer for factorial (press Q to exit). help(factorial)
Near the bottom of the code is a loop that fires 6 times. Change 6 to some other small integer, such as the number of letters in your first name. At the top of the file is a comment giving the revision date and a list of authors. Change the date to today's date, and add yourself as an author. Save the document as intro.py
.
Now we're going to run the program. Open Terminal. Navigate to the directory (folder) where you saved intro.py
, using these basic Unix shell commands:
pwd
tells you what directory you're currently in, in the hierarchy of all directories and files on the computer.
ls
lists the contents of the current directory.
cd directory
(where directory
is the name of some directory inside your current directory) changes your current directory. That is, it takes you "down", deeper into the hierarchy.
cd ..
takes you "up" to the directory that contains your current directory.
cd
takes you back to your home directory; use this if you get lost.
python intro.py
to run your Python program. It should print out some numbers and then launch the help viewer. When you want to exit the help viewer, press Q; then you can see the numbers again. Make sure that you understand what every line of code does. If you don't, then ask someone for help.
Finally, submit your work using the following steps. Warning: Once you've submitted a file, you cannot edit or delete it.
Go
menu, then Connect to Server...
.
courses.its.carleton.edu/COURSES
and press Return.
f10
, then cs
, then cs201-02-f10
, then Student Work
.
hand-in
directory inside it.
intro.py
into your hand-in directory.
The code I gave you above exhibits what I call Good Python Style:
import
statements. That is, import
statements aren't sprinkled throughout the file; they're all at the top. This is so that anyone who reads this file can quickly determine which other modules it uses.
"""
, that comes immediately after the function, class, or method declaration and describes how to use that function, class, or method. Like a comment, a docstring tells the reader how to use the function/class/method. Unlike a comment, a docstring is viewable in the Python help system.
if __name__ == "__main__":
. The demo code runs if the user executes the module directly, but does not run if the user imports the module into another module.
import
statements, all top-level code is in the demo section. In other words, the only stuff allowed outside the demo section are comments, import
statements, function definitions, and class definitions.
Please send me a brief e-mail message with the following information. (My address is at the top of the page.)