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 :
A few good ones to start:
- 1 "Find the sum of all the multiples of 3 or 5 below 1000."
- 4 "Find the largest palindrome made from the product of two 3-digit numbers."
- 9 "There is one Pythagorean triple (i.e. a2 + b2 = c2) with a + b + c = 1000. Find a*b*c."
- 55 An investigation of Lychrel numbers.
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.