{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

repetition-5

# repetition-5 - CMSC 143 Keith OHara [email protected] CMSC143...

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

Sep 15 2010 CMSC143 - O'Hara 1 1 CMSC 143 Repetition and Recursion Keith O’Hara [email protected]

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

View Full Document
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()

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

View Full Document
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 0 , p 2 , …, p n ): statement statement return value z = function(a 0 , a 2 , …, a n )

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

View Full Document
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: doesn’t return anything def movie( frames ): if frames > 0: p = takePicture() turnLeft(0.5, 0.5) show( p ) movie( frames - 1) movie(25)

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

View Full Document
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: doesn’t 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

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

View Full Document
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
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 35

repetition-5 - CMSC 143 Keith OHara [email protected] CMSC143...

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

View Full Document
Ask a homework question - tutors are online