Introductory Programming Fall 2004 For Thursday you should 1) Read Section 9.1 -- 9.4 and Chapter 10 Homework 5 Solution ------------------- move and nudge are fruitless functions. They do stuff (like move the Amoeba) but they have no return statement. The return value from a fruitless function is a special value called None. def move(a, limit=10): # move the amoeba by a random amount x = a.x + random_coordinate(1) y = a.y + random_coordinate(1) x = bound(x, limit) y = bound(y, limit) teleport(a, x, y) def nudge(a, b, fraction=0.02): # move a just a little bit closer to b dx = a.x - b.x a.x -= dx * fraction dy = a.y - b.y a.y -= dy * fraction You use fruitless functions as if they were statements: move(amy) move(bob) nudge(amy, bob) nudge(bob, amy) distance and bound are fruitful functions. They have no visible effect, but they return values. def distance(a, b): # compute the distance between amoebas dx = a.x - b.x dy = a.y - b.y return sqrt(dx*dx + dy*dy) def bound(x, limit): # make sure that x is between -limit and +limit if x > limit: x = limit if x < -limit: x = -limit return x You use fruitful functions as part of an expression: x = bound(x, limit) y = bound(y, limit) and d = distance(amy, bob) if d<1.0: mate(amy, bob, world, n-i) This homework involved some code you may not have understood, which I think made it hard to get started. I hope that Homework 6 is easier to get started on. Evaluation 5 ------------ Let's see how this went and behave accordingly! Homework 6 ---------- Let's work on this in class and let me help people out a little.