Log in to save your progress.
<< Previous
Home
Next >>
Note! The window is too narrow to use Pythonlab.dev. Use a device with a keyboard!

Reference

Click on a function to read more.
Turtle
Function
Short description
back()
Moves the turtle backward
begin_fill()
Starts filling with color
circle()
Draws a circle with optional radius
color()
Determines the color the turtle draws with
end_fill()
Ends filling with color
forward()
Moves the turtle forward
goto()
Moves the turtle to a given coordinate
hideturtle()
Hides the turtle
left()
Turns the turtle left a number of degrees
pendown()
Puts the pen down on the drawing area.
penup()
Lifts the pen from the drawing area
right()
Turns the turtle right a number of degrees
shape()
Changes the drawing shape
Screen().setup()
Creates a drawing area with a given size
speed()
Sets the turtle's drawing speed

Drawing with random numbers

Here you will learn

Generate random numbers

To create random numbers, we will use the function randint which is in the module random. Randint is a combination of two abbreviations, rand for random and int for integer.

By writing from random import randint, randint is imported from the random module. This function takes two arguments, randint(start, stop) where start is the smallest random number that can be generated and stop is the largest random number that can be generated.

If you want to read more about how random numbers are generated, see the basic course on random numbers.

Example

Draws a line with a random length. A random number between 10 and 200 is saved in the variable length.

Try running the code several times.

from random import randint

#randomize a number between 10 and 200        
length = randint(10,200) 
forward(length)

      

Login to save your progress and your code.

Create

Draw a circle with a random radius in the interval [10,100]. To remember how to draw circles, see the section on circles.

Random walk

Now we will let our turtle wander randomly on the drawing area. The turtle starts as usual in the middle. Then it takes repeated steps in a random direction.

Random walk is an established scientific concept and can also be called Brownian motion. Read more on wikipedia.

Example

Random walk with 500 steps.

from turtle import *
from random import randint
shape('turtle')

for i in range(200):
    direction = randint(1, 360)
    left(direction)
    forward(10)
  

Login to save your progress and your code.

Modify the code

The code below is the same as in the example above. Change it so that the turtle takes half as many steps, but twice as long steps. Also let the turtle draw with the color green.

The RGB color system

So far we have used a text string as an argument to color, such as color('green'). It is also possible to determine the color according to the RGB system. RGB stands for red, green, blue.

The color is set with color(red, green, blue) where the arguments red, green and blue are numbers in the interval [0,1] or [0,255]. To use the interval [0,255] we need to write colormode(255) at the beginning of the code.

For example, the first argument stands for how much red should be in the color, color(255,0,0) therefore means maximum red with no green and no blue. The color will therefore be red.

Example

A triangle with three different colors. Feel free to experiment with the colors!

colormode(255)

color(255,0,0) #red
forward(100)
left(120)
color(0,255,0) #green
forward(100)
left(120)
color(0,0,255) #blue
forward(100)

  

Login or create an account to save your progress and your code.

What does the code do?

Read the code below and try to figure out what the program draws. Run the program after you answer and see if you were correct.

Question: What will the program draw?

A random walk with increasing red color
A square with random colors on the sides
A random walk with random colors
A square with increasing red color

Login to save your progress and your code.

Modify the code

The program below draws 100 squares that cover the entire drawing area. Modify the function colored_square so that the squares get different random colors. Remember that the numbers in the RGB system are in the interval [0,255].

Status
You have finished all tasks in this section!
🎉