# Python Course 2: Bracelets and Snakes

The following code draws a snake

``````import turtle

SIDE = 50

def triangle():
turtle.begin_fill()
for n in range(3):
turtle.forward(SIDE)
turtle.right(120)
turtle.end_fill()

def black_triangle():
turtle.fillcolor("black")
triangle()

def white_triangle():
turtle.fillcolor("white")
triangle()

def snake():
black_triangle()
turtle.forward(SIDE)
turtle.right(60)
white_triangle()
turtle.left(60)

snake()``````

## Exercise

1. Copy the code into your IDE and run it.
2. Draw a snake 6 segments long
3. Draw a red and green snake, 6 segments long.
4. Draw pink and yellow snake, 6 segments long, but half the size of the previous snake.
5. Use a for loop to simplify your code.
6. Now draw a snake 10 segments long

## Extension

Modify your code to draw a bracelet, as shown below.

# Functions and Turtle Graphics

The following code is the start of a solution to the problem of  drawing a chessboard. It uses functions to help make the code  understandable

``````import turtle

# Declare this at the top.  Put it in capitals to show it shouldn't be changed
SIDE = 50

def square():
turtle.begin_fill()
for n in range(4):
turtle.forward(SIDE)
turtle.right(90)
turtle.end_fill()

def black_square():
turtle.fillcolor("black")
square()

def white_square():
turtle.fillcolor("white")
square()

def move_one_square_right():
turtle.penup()
turtle.forward(SIDE)
turtle.pendown()

def move_one_square_down():
turtle.penup()
turtle.right(90)
turtle.forward(SIDE)
turtle.left(90)
turtle.pendown()

def white_black_line():
for n in range(4):
white_square()
move_one_square_right()
black_square()
move_one_square_right()

# Speed up the turtle
turtle.speed(0)

# Draw two lines of the chessboard
white_black_line()
turtle.home()
move_one_square_down()
move_one_square_down()
white_black_line()``````

## Exercise

1. Copy the code into your IDE. Watch out for indents!
2. Run the code and see what it does.
3. Add a function to draw a black_white() line
4. Draw a chessboard using the functions provided
5. Can you make you code more efficient, perhaps by using a for loop?
6. What happens if you change the value of SIDE?
7. Draw a 10×10 chessboard

## Extension

The following function draws a hexagon:

``````def hexagon():
for n in range(6):
turtle.forward(50)
turtle.left(60)``````
1. Use the function to draw a honeycomb pattern
2. Can you make the honeycomb cover the whole screen?

This site uses Akismet to reduce spam. Learn how your comment data is processed.