3: DRY Turtle
Expectations
Learning Targets
I can implement the basic constructs of programming languages such as variables, conditionals, loops, and flow control.
I can make a class wrapper that inherits from a library's class.
I can call a method from a library.
Assessments
You will be submitting Trinket.io scripts demonstrating the following skills.
Class Videos
Class 1/24 Turtle drawing assignment:
What is DRY code?
Loops and functions allow skilled programmers to make more efficient code that's easier to maintain.
Parts of a for loop
A for loop has an iterator and a collection that it traverses.
Parts of a function
A function has a definition that lists any parameters and has a code block underneath that gets activated when called.
In the example above, the magic_spell
function isn't a part of a Class. It's on its own. If that ability was a part of a player in a game, we'd call it a method instead of function.
Code Along
We're going to jump into work with a lot of advanced concepts. This is intentional. New programmers are very often intimidated by code they don't understand. I want you to get used to that early on. Enjoy tinkering with a few lines here and there and see how things change. Get comfortable swimming in the deep end.
This class (inherits) from the Turtle class which is available since we imported the turtle library. Instead of writing from turtle import Turtle
I could have also written just import turtle
in which case every time I wanted to use the Turtle class, I would have had to specify the library, such as turtle.Turtle()
The only thing that our SuperTurtle
does that the regular Turtle doesn't is the way it gets constructed. When a new Turtle is created or instantiated, we initialize or __init__
with some default properties.
Follow along as we go over a couple of skills. You aren't expected to be able to do this stuff yourself quite yet.
Make a loop and create a bunch of turtles
Add a new method to the turtle to make it go to a random location and call that method in the constructor
Make a list of colors and make each turtle randomly select a color
Make an empty list and add each turtle to the collection as it's being instantiated
Loop through the turtle collection and modify each
We'll spend some time experimenting with this code, learning by modifying existing code.
Building Methods
Experimenting with code given to you is fun and can be very helpful, but let's slow things down now and build some basic skills. Your Trinket.io app should look something like this:
A method or function is a group of commands that can be called. If I tell you to please clap, that's me activating or calling the command that's already defined in your head. I may give you additional parameters or arguments like, "clap 5 times loudly". In Python that may input("What's your name?")
but that doesn't save the variable, so we write it as:
name = input("What's your name?")
written as joe.clap(5, "loudly")
What's the difference between a function and a method?
They both start with def
and will execute their pre-programmed commands when called. However, we'll refer to a function as something floating outside of a class and a method as an ability built within an object. For example, a method would be if an instance of the Human class, let's call him Joe, had to sneeze: joe.sneeze()
. But a function would be an anonymous helper like therancolor()
function programmed above that just returns a random string in a list of colors.
Novel Design
Look at the documentation, other student examples, and Google searches to modify open-source code and customize your own design. You can use one or many turtles but the end result has to have multiple colors over a fun design.
Taking Controls
Let's make our app more interactive. Just like the print("message")
function we've been using, there's another one that takes a string input called input()
input("What's your name?")
will work but that doesn't save the user's response anywhere. So let's create a variable that will store the answer in this example:
name = input("What's your name?")
Testing the input
To make sure we are asked the question over again, we'll put the input within a while True:
loop.
Last updated