From the course: Python: Programming Efficiently

Unlock the full course today

Join today to access over 23,300 courses taught by industry experts.

Solution: Bale of turtles

Solution: Bale of turtles

(upbeat music) - [Instructor] We begin by importing all the modules that we need. Our new class, Bale, needs an initializer that creates the required number of turtles. We make them by calling turtle repeatedly with the same terrarium and collecting the resulting instances with the list comprehension. We assign the list to the instance attribute, turtles. Note that we're using an f-string with the loop variable I to give each turtle a different color. Since MatLog Libre defines 10 standard colors named C zero through C nine use the module operator to cycle through them if we are making more than 10 turtles. Next, we write a forward method for Bale. We first check if the parameter distance is an iterable, in that case, we loop over the turtles and the parameters together and call the method forward of each turtle with a corresponding parameter. If distance is just a number, we loop over the turtles only and call…

Contents