Oct 4
old business
Any questions about anything we've done so far?
finish discussion of booleans
Finish up Tuesday's lecture notes, particularly
talking briefly about truth tables. (There's an assignment along these lines.)
Write a program to loop through assignments of (True, False) to (x,y)
to see which (if any) of these are related. (Do this by hand first on the board.)
(not x) and (not y)
not (x and y)
not (x or y)
(not x) or (not y)
Compare with how related "expansions" behave in multiplication and addition :
a * (b + c) <=> a * b + a * c
Can these be transformed? If so, how?
(x or y) and (a or b)
(x and y) or (a and b)
practice
Work 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."
- 14 "Collatz numbers" (google xkcd collatz)
- 55 "An investigation of Lychrel numbers."
try, catch
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.
The code we created in class is attached.