Intro to
Programming
with Python

Fall 2011
course
navigation

oct 6

homework

First, let's look at some programs that could use some rewriting; see attached.

practice

Second, do some problems in class. While they're pretty math-geeky, this site has many good programming problems :
projecteuler.net
A few good ones to start:

try, catch

Third, discuss errors and how to handle them in Python :
try: x = int(raw_input("What is x? ")) except ValueError: print "You must enter a number."
This is like an (if: ... else: ...) construct, but it branches on system errors rather than boolean tests.
To figure out what to put after the "except" part, do something at the interactive prompt to make the error happen.
Sometimes this is called "throwing" and exception and "catching" it.
Class exercise: use the code above as the basis for a function get_integer() which asks the user for an integer, and handles errors gracefully.

in class

We created the work1.py, work2.py, euler1.py, euler4.py attached programs.
http://cs.marlboro.edu/ courses/ fall2011/python/ notes/ oct_6
last modified Thursday October 6 2011 3:01 pm EDT

attachments [paper clip]

     name last modified size
   euler1.py Oct 6 2011 3:00 pm 896B    euler4.py Oct 6 2011 3:00 pm 803B    functions_1_needs_work.py Oct 5 2011 8:27 pm 855B    functions_2_needs_work.py Oct 5 2011 8:27 pm 3.08kB    get_integer.py Oct 5 2011 8:52 pm 314B    work1.py Oct 6 2011 2:53 pm 1.03kB    work2.py Oct 6 2011 3:00 pm 1.26kB