Intro to
Programming
with Python

Fall 2012
course
navigation

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 :
projecteuler.net
A few good ones to start:

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.
http://cs.marlboro.edu/ courses/ fall2012/python/ notes/ Oct_4
last modified Thursday October 4 2012 11:23 am EDT

attachments [paper clip]

     name last modified size
   euler_1.py Oct 4 2012 11:22 am 830B    get_integer.py Oct 4 2012 11:23 am 324B    truth_table.py Oct 4 2012 11:22 am 672B