“Bring to Class” Instructions
Bring the code you write for the below exercise to the following class. There are many ways you can do this.
This is the easiest:
- Sign up for a free Trinket account, create a new trinket, and do the work there. Note: you’ll want to press Save before sharing a link or getting embed code to ensure that others see the latest version of your code.
If you don’t want to create a Trinket account for any reason, here are some options:
- Use the Share button to get a link to your work. Note: you’ll want to get a new link after making any changes. Bookmark or save the link somewhere convenient.
- Copy and paste the code out of trinket, then paste it into a trinket the day of class.
- Download the trinket code and then copy and paste it into a trinket the day of class.
There is nothing to turn in, but you will want to hang on to this code so that you can reference or embed it in future reflections.
Note: For this exercise, embed both the original program and your refactor of it in the post. See below for instructions on duplicating your trinkets
Choose a previous Turtle program you’ve made and refactor it to use at least two custom functions
(i.e. made by you using the def
keyword, not built-in).
At least one of these functions should take arguments and at least one should return something.
For bonus (karma/style) points, use some functions you have to import from the random
or math
modules.
When choosing a turtle program, one that has lots of repetitive code with slight variations is a good candidate. For instance, a program that goes to many x,y coordinates and draws a circle would be a good fit to get re-written using this function:
def go_draw_circle(x, y):
tina.penup()
tina.goto(x,y)
tina.circle(10)
tina.pendown()
You have considerable discretion in what gets parameterized in your function. Parameterization is the abstract concept of making something a function of an input, or parameter. Specifically, ‘arguments’ are parameters to ‘functions’ in Python. For example, my function always draws size 10 circles. I could re-write it with circle size as an argument to the function and have control over that:
def go_draw_circle(x, y, size):
tina.penup()
tina.goto(x,y)
tina.circle(size)
tina.pendown()
Don’t make an entirely new trinket from scratch. If there comes a point when you want to dramatically expand on what your trinket does, Make a Copy of your trinket so that you have your refactored turtle and anything you did to it afterwards.. We’ll do more of that later. Refactoring, technically, ends up with a program that does the exact same thing, just written more efficiently. In many cases, people also improve as they refactor, but keep that to a minimum this time. Or, if you get excited about your newfound powers, make sure to save a copy after the refactor.
Trinket Duplication
In trinket, you can Duplicate the trinket you want to refactor using the Duplicate button:
You can also copy and paste code, and use the Share button to get new links.