repetition-5 - Sep 15 2010 CMSC143 - O'Hara 1 CMSC 143...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Sep 15 2010 CMSC143 - O'Hara 1 CMSC 143 Repetition and Recursion Keith OHara kohara@bard.edu Sep 15 2010 CMSC143 - O'Hara 2 Quiz def f ( x ): if x < 0: return -1 elif x > 0: return 1 else: return 0 print f(-1) + f(-100) print f(-1) + f(1) print f(1) + f(2) Multiple Robots - Objects You can control multiple robots from one python shell Use the object interface to the robot Methods functions on objects Sep 15 2010 CMSC143 - O'Hara 3 alice = Scribbler(com30) bob = Scribbler(com31) alice.forward(0.75, 1) bob.beep(1, 440) apic = alice.takePicture() bpic = bob.takePicture() computer.beep(1, 440) computer.playSong() Sep 15 2010 CMSC143 - O'Hara 4 Recursion : See RECURSION. Sep 15 2010 CMSC143 - O'Hara 5 Functions in general # description of this function # input: what it expects as input # output: what is provides as output def function (p , p 2 , , p n ): statement statement return value z = function(a , a 2 , , a n ) Sep 15 2010 CMSC143 - O'Hara 6 Variables in a Function are Local Variables in a function are private Including the parameters Each function call has its own copy of the local variables. Sep 15 2010 CMSC143 - O'Hara 7 Functions that call themselves! Moving Moving Pictures Recursive functions call themselves Totally Crazy! Totally Powerful! # Takes and shows a series of # pictures # input: frame number of frames # output: doesnt return anything def movie( frames ): if frames > 0: p = takePicture() turnLeft(0.5, 0.5) show( p ) movie( frames - 1) movie(25) Sep 15 2010 CMSC143 - O'Hara 8 Flow of Control - Stack Diagrams def printHello( times ): print hello * times def startPrintHello( n ): print Starting hello printHello( n ) startPrintHello(4) Top-level printHello times = 4 Stack Diagram startPrintHello n = 4 Sep 15 2010 CMSC143 - O'Hara 9 Moving Moving Pictures # Records and plays a movie # while rotating # input: frame number of frames # output: doesnt return anything def movie( frames ): if frames > 0: print frame #, frames turnLeft(0.5, 0.5) p = takePicture() show( p ) movie( frames - 1) movie(25) Stack Diagram Top-level movie frames = 25; p = movie frames = 24; p = movie frames = 0 Sep 15 2010 CMSC143 - O'Hara 10 Moving Moving Pictures - Reverse # Records a movie while rotating # and then plays it backwards # input: frame number of frames # output: doesnt return anything def movie(...
View Full Document

This note was uploaded on 10/14/2010 for the course CMSC 143 taught by Professor Keitho'hara during the Spring '10 term at Bard College.

Page1 / 35

repetition-5 - Sep 15 2010 CMSC143 - O'Hara 1 CMSC 143...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online