Here you will learn
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.
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.
Draw a circle with a random radius in the interval [10,100]. To remember how to draw circles, see the section on circles.
-- Output from your program will be here --
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.
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.
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.
-- Output from your program will be here --
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.
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.
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.
-- Output from your program will be here --
Question: What will the program draw?
Login to save your progress and your 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].
-- Output from your program will be here --