####################################################### # GRAPHICS: ####################################################### # see link: http://www.kosbie.net/cmu/spring-13/15-112/ ####################################################### # ANIMATIONS: ####################################################### # Elements of an Event-Based Program: # 1) run() # creates canvas and canvas.data; registers event handlers; calls init; starts timer; etc # You call run() to start program # 2) init() # initialize values in canvas.data # 3) mousePressed(event) # extract mouse location via event.x, event.y # modify canvas.data based on mouse press location # call redrawAll if view changed # 4) keyPressed(event) # extract char and/or keysym from event.char and event.keysym # modify canvas.data based on key pressed # call redrawAll if view changed # 5) timerFired() # modify canvas.data based on elapsed time # call redrawAll if view changed # all canvas.after() to schedule next timerFired event (after a delay of your choice) # 6) redrawAll() # clear the canvas with canvas.delete(ALL) # redraw all canvas elements from back to front based on values in canvas.data # -----> see kesden.zip online for recitation example!!! ####################################################### # FUNCTIONS AS ARGUMENTS/FUNCTIONS IN FUNCTIONS: ####################################################### # given this: def done(value): print "DONE!" return (value / 10) def one(x, func): print "one!" x += 1 def two(y): print "two!" y += 1 if not (y % 10): print "found it!" return True return False test = two(x) if test: def three(z): print "three!" return (z + 1) x = three(x) else: def four(z): print "four!" return 0 x = four(x) return func(x) # what will the following print? # return one(18) # why is this wrong??? print one(18, done) print print one(10, done) ####################################################### # FUNCTIONS WRAPPERS (A.K.A. CONTRACTS): ####################################################### # given this: def contract(func): def check(x, s): if (x < 0) or (x >= len(s)): print "index must be positive and within string!" return else: return func(x, s) return check @contract def printIndex(x, s): return s[x] # what will the following print? print print printIndex(0, "abcd") print printIndex(-1, "abcd") print printIndex(12, "abcd") print ####################################################### # ARGUMENT LISTS: ####################################################### # *args -> take all arguements and pass in as a list # OR # -> take a list of arguements and make them # into individual arguemnts def func_1(*args): final = "" for s in args: final += s print final func_1("hey ", "there ", "buddy!") def func_2(a, b, c, d): if ((a + b + c + d) <= 10) and \ (a > 0) and (b > 0) and (c > 0) and (d > 0): return a + b + c + d else: return "invalid input" test = [1, 2, 3, 4] print func_2(*test) test.remove(1) test.append(4) print func_2(*test)