Everything you have written so far prints text. Turtle draws pictures instead. It comes with Python — nothing to install.
You control a pen that moves around a window. Tell it to go forward, turn, and change color, and it leaves a line behind it. That is the whole idea.
This pairs with Activity 1.16 For Loops on purpose. Drawing a square by hand takes eight lines. Drawing it with a loop takes three, and once you can loop, you can draw things that would be miserable to write out by hand.
Create a file named le-turtle and run this:
import turtle
turtle.forward(100)turtle.right(90)turtle.forward(100)
turtle.done()A window should open and draw two lines meeting at a right angle. turtle.done() on the last line is what keeps the window from closing the instant the program ends — leave it off and you will see a flash of nothing.
If no window appears at all, turtle needs a piece of Python called tkinter, and some installations leave it out. Tell me and we will sort it out — it is not something you did wrong.
Four sides, four identical turns. That is a for loop wanting to happen.
import turtle
for side in range(4): turtle.forward(100) turtle.right(90)
turtle.done()Note that side is never used inside the loop. That is fine — sometimes you only want the loop to repeat, not to count.
import turtle
turtle.speed(0)turtle.pencolor("cyan")turtle.width(3)
for side in range(6): turtle.forward(120) turtle.right(60)
turtle.done()turtle.speed(0) means "as fast as possible" — you will want it once your drawings get bigger. Try changing the 6 and the 60 together and see what shapes you get.
Here is the pattern worth knowing: for any regular shape, the turn is 360 divided by the number of sides. Six sides, 60 degrees. Eight sides, 45. Work out why.
Now use the loop variable instead of ignoring it. Each pass draws a slightly longer line than the last.
import turtle
turtle.speed(0)
for step in range(90): turtle.forward(step * 3) turtle.right(89)
turtle.done()Change the 89 to 90 and run it again. One degree of difference changes the whole picture — that is worth staring at for a moment.
Draw something of your own. Ideas, in rough order of difficulty:
input() and castingTo submit: your .py file and a screenshot of what it drew.