First steps with Python programming

Going Loopy

The solution, as you might already have guessed, is to write a program that repeats the draw and turn instructions as many times as needed. Here it is:

import turtle
for g in range(4):

The second line in this program tells Python you want to repeat the next bit four times; in fact, it does more than that. It creates a variable (i.e., a place for storing information) and calls it g. It puts the numbers 0, 1, 2, and 3 into that variable in turn each time the loop repeats.

Look at the colon at the end of the second line. When you press Return after it, the next line is automatically indented for you. The way you tell Python which instructions belong to the for command is by indentation. The convention is to use four spaces, and the program won't work if you don't use indentation consistently. By forcing you to indent, Python makes sure your programs (and everyone else's) are easy to read.

Parts of a program that repeat are often called loops. The loop is much easier to work with than all those individual drawing commands. If you decide to draw a triangle instead of a square, for example, you can just change the for command to repeat 3 times, and the angle of the turn to 120 degrees.

A Touch of Color

If you want to change the pen color, you can do so in several different ways. One way is to tell the turtle module how much red, green, and blue to mix into a final color. The maximum amount of blue, with no green or red, would be pure blue, for example, and the maximum amount of red and green with no blue produces yellow.

To make the turtle draw yellow lines, use:

turtle.pencolor(255, 255, 0)

The first line only needs to be put in your program once to tell the turtle module the format in which you want to specify colors. The second line gives the amount of red, green, and blue (in that order) on a scale from 0 to 255, and you can use it whenever you want to change the pen color – try it out by experimenting with different numbers.

The word "color," you'll notice, is spelled the US way here. It won't work if you slip a "u" into it.

Buy this article as PDF

Express-Checkout as PDF

Pages: 5

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Pi with Sugar and turtles as a learning tool

    Despite its popularity elsewhere, the Raspberry Pi is first and foremost an educational tool designed with primary and secondary students in mind. We look at how teachers can make the Pi fulfill this role with the help of the Sugar Educational Desktop and turtle graphics.

  • Python Primer

    Your Raspberry Pi comes with Python, a flexible open source programming and scripting language. Download this PDF for a quick tutorial on getting started with Python.

  • Using Scratch to explore turtle geometry

    We show you how to use some simple Scratch concepts to create shapes and patterns.

  • How to get your Pi to go

    Pi2Go-Lite is a new fully integrated robot kit that makes it easy to get into robotics with the Raspberry Pi. We speak to its creator about how it works, then build our own autonomous line-following robot.

  • Math, Music, and Cat Toys

    Welcome to Raspberry Pi Geek – the first and only print magazine dedicated to the amazing Raspberry Pi mini-PC and the open hardware revolution. We ring in the new and old in this issue. (Actually, nothing is really very old with the Raspberry Pi, but we follow up on some previous themes, including a report on how it went for the wind-turbine-powered Raspberry Pi we described last time.)