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 - Python Tutorial
From the course: Python: Programming Efficiently
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Object-oriented and functional Python1m 51s
-
(Locked)
Divide and conquer with Python classes6m 35s
-
(Locked)
Exploit class inheritance5m 58s
-
(Locked)
Functional techniques in Python6m
-
Function decorators5m 31s
-
(Locked)
Challenge: Bale of turtles2m 29s
-
(Locked)
Solution: Bale of turtles3m 8s
-
(Locked)
-
-