O'Reilly logo

Genetic Algorithms and Machine Learning for Programmers by Frances Buontempo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Let’s Find the Bottom of the Bag

Make a class to demonstrate a turtle moving around the paper bag:

1: class​ Demo:
def​ ​__init__​(self, f):
self.alex = turtle.Turtle()
self.alex.shape(​"turtle"​)
5:  self.f = f
def​ ​bag​(self, points):
line = turtle.Turtle()
line.pen(pencolor=​'brown'​, pensize=5)
10:  line.up()
line.goto(points[0], self.f(points[0]))
line.down()
for​ x ​in​ points:
line.goto(x, self.f(x))
15:  line.hideturtle()
def​ ​start​(self, x):
self.alex.hideturtle()
self.alex.up()
20:  self.alex.goto(x, self.f(x))
self.alex.down()
self.alex.showturtle()
self.alex.pen(pencolor=​'black'​, pensize=10)
self.alex.speed(1)
25: 
def​ ​move​(self, x, y, jump=False): ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required