Introductory Programming Fall 2004 For next time you should: 1) Finish Homework 4. 2) Finish Chapter 5. Boolean operators ----------------- The book is a little out of date: >>> 5 == 5 True >>> 5 == 6 False As of Python 2.3, conditional operators yield the special values True and False. Other conditional operators are > < >= <= != <> And here's a trick most languages can't do... >>> 5<6<7 True Logical operators ----------------- not inverts a boolean expression >>> not 1==1 False x and y is True if both x and y are True >>> 1==1 and 2==2 True x or y is True if x or y is True, or both are >>> 1==1 or 1==2 True Conditionals ------------ Most often, boolean expressions are used in if statements if x == 5: do(something) else: different(something) Variations: 1) conditional execution 2) alternative execution 3) chained conditional 4) nested conditional Homework 4 Tips --------------- 1) recursive draw() 2) mating amoebas Amoeba() is a function that creates an Amoeba object amy = Amoeba() bob = Amoeba() Objects have attributes: amy.x = 100 amy.y = 100 To make the Amoeva move, we have to redraw it: amy.redraw() So that's how teleport works: def teleport(a, x, y): # move amoeba a to the given location a.x = x a.y = y a.redraw(a.x, a.y) Your job is to add code in move to keep the amoebas in bounds. def move(a, limit=10): # move the amoeba by a random amount x = a.x + random_coordinate() y = a.y + random_coordinate() teleport(a, x, y) And to write a function named nudge to bring them together. Then, in main, check whether the Amoebas found each other... 1) compute distance 2) check if it is less than 1.0 3) do something appropriate (in both senses of the word!)