An algorithm is a sequence of steps that performs a task. In order to sucessfully program you must take the task that’s assigned and break it down into steps. When you have an idea of the steps required begin writing code. This notebook will use the
pen class which will help you understand algorithms.
How can you use the pen to draw shapes? Every shape is a sequence of pen strokes and turns. For example, think of a triangle. You can draw a triangle by:
Drawing an initial line
Turning 120 degrees
Drawing a second line
Turning 120 degrees
Trawing a third line
Every closed shape has one important thing in common, the sum of all the turns equals 360 degress. A triangle has three turns of 120 degrees each. The code below draws a triangle.
from p4e.drawing import Turtle
You only have to run the
import cell once unless you restart the python kernel.
tu = Turtle() display(tu)
tu.clear() tu.draw(80) # Draw the first line tu.turn(120) tu.draw(80) # Draw the second line tu.turn(120) tu.draw(80) # Draw the third line tu.turn(120)
Part 1: Drawing Algorithms#
In the cells provided write code to implement the requested algorithms.
1. Draw a Square#
Write an algorithm that draws a square.
2. Draw a Star#
Write an algorithm that draws a five pointed star. Hint: the sum of the angles should add to 720.
3. Draw a “T”#
Write an algorithm that draws a capital “T”
4. Draw an “A”#
Write an algorithm that draws a capital “A”
5. Escape the Maze!#
Use pen commands to help the turtle escape the maze.
tu = Turtle() tu.background('files/maze.png') display(tu)
Part 2: Explore Turtle Commands#
Use the Lesson 2 notebook to discover how to solve the challenges in this section.
1. Draw an “M”#
goto Turtle function to draw an “M”. Do not use ``draw`` or ``turn``.
2. Draw an “X”#
goto function to draw an “X”. You will need to pick the pen up and put it down again. Do not use ``draw`` or ``turn``.
3. Colorful Writing#
Write your name three times using three different colors.