CodeHS - Intro to Computer Science in Python ( Rainforest )

- Introduction to Programming With Turtle Graphics -

- 2.1.4: Stretched Slinky

circle ( 35 )

forward ( 40 )

circle ( 35 )

forward ( 40 )

circle ( 35 )

forward ( 40 )

circle ( 35 )

forward ( 40 )

circle ( 35 )

forward ( 40 )

- 2.2.4: Shorter Dashed Line

penup ( )

backward ( 200 )

pendown ( )

forward ( 50 )

penup ( )

forward ( 50 )

pendown ( )

forward ( 50 )

penup ( )

forward ( 50 )

pendown ( )

forward ( 50 )

penup ( )

forward ( 50 )

pendown ( )

forward ( 50 )

penup ( )

forward ( 50 )

- 2.2.5: Caterpillar

circle ( 20 )

penup ( )

forward ( 40 )

pendown ( )

circle ( 20 )

penup ( )

forward ( 40 )

pendown ( )

circle ( 20 )

penup ( )

forward ( 40 )

pendown ( )

circle ( 20 )

penup ( )

forward ( 40 )

pendown ( )

circle ( 20 )

penup ( )

forward ( 40 )

pendown ( )

- 2.3.5: Rectangle

forward ( 50 )

left ( 90 )

forward ( 100 )

left ( 90 )

forward ( 50 )

left ( 90 )

forward ( 100 )

left ( 90 )

- 2.3.6: 4 Columns

penup ( )

forward ( 200 )

left ( 90 )

forward ( 200 )

left ( 90 )

forward ( 100 )

left ( 90 )

pendown ( )

forward ( 400 )

penup ( )

right ( 90 )

forward ( 100 )

right ( 90 )

pendown ( )

forward ( 400 )

left ( 90 )

penup ( )

forward ( 100 )

left ( 90 )

pendown ( )

forward ( 400 )

- 2.4.5: Row of Circles

penup ( )

backward ( 200 )

pendown ( )

for i in range ( 20 ) :

penup ( )

forward ( 10 )

pendown ( )

circle ( 10 )

penup ( )

forward ( 10 )

backward ( 400 )

- 2.4.6: 4 Columns 2.0

penup ( )

forward ( 200 )

left ( 90 )

forward ( 200 )

for i in range ( 2 ) :

penup ( )

left ( 90 )

forward ( 100 )

left ( 90 )

pendown ( )

forward ( 400 )

penup ( )

right ( 90 )

forward ( 100 )

right ( 90 )

pendown ( )

forward ( 400 )

- 2.5.5: Hexagon

for i in range ( 6 ) :

forward ( 50 )

left ( 60 )

- 2.5.6: 'X' Marks the Spot

left ( 45 )

for i in range ( 4 ) :

forward ( 100 )

backward ( 100 )

left ( 90 )

- 2.5.7: Circle Pyramid

speed ( 10 )

penup ( )

setposition ( - 100 , - 200 )

pendown ( )

for i in range ( 3 ) :

circle ( 50 )

penup ( )

forward ( 100 )

pendown ( )

penup ( )

setposition ( - 50 , - 100 )

for i in range ( 2 ) :

pendown ( )

circle ( 50 )

penup ( )

forward ( 100 )

setposition ( 0 , 0 )

pendown ( )

circle ( 50 )

- 2.6.4: Circle Pyramid with Comments

"""

This code will draw six circles stacked up like a pyramid.

Each circle has a radius of 50

"""

speed ( 10 )

#For the first 3 circles

penup ( )

setposition ( - 100 , - 200 )

pendown ( )

for i in range ( 3 ) :

circle ( 50 )

penup ( )

forward ( 100 )

pendown ( )

#For the next 2 circles

penup ( )

setposition ( - 50 , - 100 )

for i in range ( 2 ) :

pendown ( )

circle ( 50 )

penup ( )

forward ( 100 )

#For the last circle

penup ( )

setposition ( 0 , 0 )

pendown ( )

circle ( 50 )

- 2.8.4: Beaded Bracelet

speed ( 0 )

penup ( )

setposition ( 0 , - 100 )

def circle_circle ( ) :

pendown ( )

circle ( 10 )

penup ( )

left ( 10 )

forward ( 20 )

for i in range ( 36 ) :

circle_circle ( )

- 2.8.5: Shape Stack

speed ( 10 )

penup ( )

setposition ( - 25 , - 200 )

pendown ( )

def square_thing ( ) :

square_in_square ( )

penup ( )

setposition ( - 25 , - 100 )

pendown ( )

square_in_square ( )

penup ( )

setposition ( - 25 , 0 )

pendown ( )

square_in_square ( )

penup ( )

setposition ( - 25 , 100 )

pendown ( )

square_in_square ( )

penup ( )

setposition ( 0 , 200 )

def square_in_square ( ) :

for i in range ( 4 ) :

forward ( 50 )

left ( 90 )

def circle_thing ( ) :

right ( 90 )

forward ( 50 )

left ( 90 )

pendown ( )

circle ( 25 )

penup ( )

right ( 90 )

forward ( 50 )

right ( 90 )

pendown ( )

circle ( 25 )

left ( 90 )

penup ( )

forward ( 100 )

right ( 90 )

pendown ( )

circle ( 25 )

left ( 90 )

penup ( )

forward ( 100 )

right ( 90 )

pendown ( )

circle ( 25 )

square_thing ( )

circle_thing ( )

- 2.9.5: Four Colored Triangles

speed ( 0 )

pensize ( 5 )

penup ( )

backward ( 100 )

left ( 65 )

def triangle_tool ( ) :

for i in range ( 4 ) :

color ( "green" )

pendown ( )

forward ( 50 )

left ( - 130 )

color ( "blue" )

forward ( 50 )

left ( 130 )

triangle_tool ( )

right ( 65 )

color ( "red" )

forward ( - 175 )

- 2.9.6: Colorful Bracelet

speed ( 10 )

penup ( )

setposition ( 0 , - 100 )

def circle_circle ( ) :

pendown ( )

color ( "blue" )

begin_fill ( )

circle ( 10 )

end_fill ( )

penup ( )

left ( 10 )

forward ( 20 )

pendown ( )

color ( "red" )

begin_fill ( )

circle ( 10 )

end_fill ( )

penup ( )

left ( 10 )

forward ( 20 )

pendown ( )

color ( "purple" )

begin_fill ( )

circle ( 10 )

end_fill ( )

penup ( )

left ( 10 )

forward ( 20 )

for i in range ( 12 ) :

circle_circle ( )

- 2.10.4: Bubble Wrap 2.0

"""

This code will fill the canvas with light blue circles.

Now add a function that will draw a white highlight on each bubble.

"""

speed ( 0 )

# This function will draw one row of 10 circles

def draw_circle_row ( ) :

for i in range ( 10 ) :

pendown ( )

begin_fill ( )

color ( "light blue" )

circle ( 20 )

end_fill ( )

penup ( )

forward ( 40 )

# This function will move Tracy from end of row up to beginning of the row on top

def move_up_a_row ( ) :

left ( 90 )

forward ( 40 )

right ( 90 )

backward ( 400 )

# Send Tracy to starting position in bottom left corner

penup ( )

setposition ( - 180 , - 200 )

# Call circle drawing function 10 times to fill ten rows

for i in range ( 10 ) :

draw_circle_row ( )

move_up_a_row ( )

penup ( )

setposition ( - 200 , - 200 )

color ( "white" )

forward ( 20 )

left ( 90 )

forward ( 20 )

right ( 90 )

forward ( 10 )

pendown ( )

left ( 90 )

# Highlights

def make_highlight ( ) :

pendown ( )

circle ( 10 , 90 )

penup ( )

left ( 90 )

forward ( 10 )

left ( 90 )

forward ( 50 )

left ( 90 )

for i in range ( 10 ) :

make_highlight ( )

right ( 90 )

move_up_a_row ( )

left ( 90 )

for i in range ( 10 ) :

make_highlight ( )

right ( 90 )

move_up_a_row ( )

left ( 90 )

for i in range ( 10 ) :

make_highlight ( )

right ( 90 )

move_up_a_row ( )

left ( 90 )

for i in range ( 10 ) :

make_highlight ( )

right ( 90 )

move_up_a_row ( )

left ( 90 )

for i in range ( 10 ) :

make_highlight ( )

right ( 90 )

move_up_a_row ( )

left ( 90 )

for i in range ( 10 ) :

make_highlight ( )

right ( 90 )

move_up_a_row ( )

left ( 90 )

for i in range ( 10 ) :

make_highlight ( )

right ( 90 )

move_up_a_row ( )

left ( 90 )

for i in range ( 10 ) :

make_highlight ( )

right ( 90 )

move_up_a_row ( )

left ( 90 )

for i in range ( 10 ) :

make_highlight ( )

right ( 90 )

move_up_a_row ( )

left ( 90 )

for i in range ( 10 ) :

make_highlight ( )

- 2.10.5: Sidewalk

speed ( 0 )

penup ( )

setposition ( - 250 , - 200 )

pendown ( )

forward ( 50 )

def square ( ) :

for i in range ( 4 ) :

left ( 90 )

forward ( 50 )

for i in range ( 4 ) :

for i in range ( 8 ) :

square ( )

forward ( 50 )

left ( 90 )

- 2.11.4: Dartboard

def mover ( ) :

penup ( )

right ( 90 )

forward ( 25 )

left ( 90 )

pendown ( )

radius = 25

circle ( radius )

mover ( )

circle ( radius+ 25 )

mover ( )

circle ( radius+ 50 )

mover ( )

circle ( radius+ 75 )

- 2.11.5: Line of Increasing Blocks

def turns ( ) :

left ( 90 )

speed ( 0 )

spaces = 20

penup ( )

setposition ( - 150 , 0 )

pendown ( )

for i in range ( 4 ) :

forward ( 10 )

turns ( )

penup ( )

forward ( spaces )

pendown ( )

for i in range ( 4 ) :

forward ( spaces )

turns ( )

penup ( )

forward ( spaces* 2 )

pendown ( )

for i in range ( 4 ) :

forward ( 30 )

turns ( )

penup ( )

forward ( spaces* 3 )

pendown ( )

for i in range ( 4 ) :

forward ( 40 )

turns ( )

penup ( )

forward ( spaces* 4 )

pendown ( )

for i in range ( 4 ) :

forward ( 50 )

turns ( )

- 2.12.4: Colored Dartboard

def circle_mover ( ) :

penup ( )

left ( 90 )

forward ( 25 )

right ( 90 )

pendown ( )

speed ( 10 )

penup ( )

right ( 90 )

forward ( 100 )

left ( 90 )

pendown ( )

color_choice = input ( "What color is this circle?" )

radius = 25

begin_fill ( )

color ( color_choice )

circle ( radius+ 75 )

end_fill ( )

circle_mover ( )

color_choice = input ( "What color is this circle?" )

begin_fill ( )

color ( color_choice )

circle ( radius+ 50 )

end_fill ( )

circle_mover ( )

color_choice = input ( "What color is this circle?" )

begin_fill ( )

color ( color_choice )

circle ( radius+ 25 )

end_fill ( )

circle_mover ( )

color_choice = input ( "What color is this circle?" )

begin_fill ( )

color ( color_choice )

circle ( radius )

end_fill ( )

- 2.12.5: Four Corners

square_length = int ( input ( "How long is one side? " ) )

def mover ( ) :

penup ( )

backward ( - 400 )

right ( - 90 )

pendown ( )

speed ( 0 )

penup ( )

setposition ( - 200 , - 200 )

color_choice = input ( "color_please " )

color ( color_choice )

pendown ( )

for i in range ( 4 ) :

for i in range ( 4 ) :

forward ( square_length )

left ( 90 )

mover ( )

- 2.13.4: Colorful Caterpillar

speed ( 0 )

penup ( )

backward ( 150 )

def make_circles ( color_choice ) :

color ( color_choice )

pendown ( )

begin_fill ( )

circle ( 20 )

end_fill ( )

penup ( )

forward ( 40 )

for i in range ( 2 ) :

make_circles ( "blue" )

make_circles ( "green" )

make_circles ( "yellow" )

make_circles ( "red" )

- 2.13.5: Circle in a Square

penup ( )

speed ( 8 )

backward ( 100 )

right ( 90 )

forward ( 100 )

left ( 90 )

pendown ( )

def square_maker ( length , pick_color ) :

begin_fill ( )

lefty = int ( input ( " How far is a turn? " ) )

for i in range ( 4 ) :

color ( pick_color )

forward ( length )

left ( lefty )

end_fill ( )

def circle_maker ( radius , pick_color ) :

penup ( )

setposition ( 0 , - 100 )

pendown ( )

begin_fill ( )

circle_color = input ( " What color is the circle? " )

color ( circle_color )

circle ( radius )

end_fill ( )

square_maker ( 200 , "blue" )

circle_maker ( 100 , "green" )

- 2.13.6: Snowman

speed ( 8 )

penup ( )

setposition ( 0 , - 200 )

pendown ( )

color ( "gray" )

def required_function ( ) :

left ( 90 )

base = int ( input ( "What is the base's radius? " ) )

begin_fill ( )

circle ( base )

end_fill ( )

required_function ( )

forward ( 2 *base )

right ( 90 )

begin_fill ( )

circle ( .5 *base )

end_fill ( )

required_function ( )

forward ( base )

right ( 90 )

begin_fill ( )

circle ( .25 *base )

end_fill ( )

required_function ( )

forward ( .5 *base )

right ( 90 )

- 2.14.4: Geometry 2.0

penup ( )

speed ( 5 )

setposition ( 0 , - 60 )

pendown ( )

setposition ( 0 , - 150 )

radius = 20

circle ( radius )

for i in range ( 7 ) :

circle ( radius*i , 360 , i )

- 2.15.4: Dartboard using i

speed ( 0 )

for i in range ( 40 ) :

circle ( i* 5 )

- 2.15.5: Phone Signal

speed ( 10 )

def turtle_move ( ) :

penup ( )

forward ( 25 )

pendown ( )

for i in range ( 4 ) :

forward ( 10 )

left ( 90 )

turtle_move ( )

for i in range ( 2 ) :

forward ( 10 )

left ( 90 )

forward ( 20 )

left ( 90 )

turtle_move ( )

for i in range ( 2 ) :

forward ( 10 )

left ( 90 )

forward ( 30 )

left ( 90 )

turtle_move ( )

for i in range ( 2 ) :

forward ( 10 )

left ( 90 )

forward ( 40 )

left ( 90 )

turtle_move ( )

for i in range ( 2 ) :

forward ( 10 )

left ( 90 )

forward ( 50 )

left ( 90 )

- 2.16.4: Happy Face

speed ( 10 )

def happy_face ( ) :

penup ( )

left ( 90 )

backward ( 100 )

right ( 90 )

begin_fill ( )

color ( "yellow" )

circle ( 100 )

end_fill ( )

setposition ( - 80 , 10 )

color ( "black" )

begin_fill ( )

circle ( 10 )

end_fill ( )

forward ( 140 )

begin_fill ( )

circle ( 10 )

end_fill ( )

setposition ( - 30 , - 30 )

pensize ( 8 )

pendown ( )

right ( 90 )

circle ( 20 , 180 )

happy = input ( "Yes or Yes? " )

if happy == "Yes" :

happy_face ( )

- 2.16.5: Black and White Squares

penup ( )

backward ( 100 )

count = 0

if count % 2 == 0 :

speed ( 0 )

def shaded_square ( ) :

begin_fill ( )

for i in range ( 4 ) :

forward ( 20 )

left ( 90 )

end_fill ( )

forward ( 30 )

def empty_square ( ) :

pendown ( )

for i in range ( 4 ) :

forward ( 20 )

left ( 90 )

penup ( )

forward ( 30 )

for i in range ( 3 ) :

shaded_square ( )

empty_square ( )

- 2.17.4: Rating

def ecks ( ) :

speed ( 10 )

color ( "red" )

pensize ( 40 )

left ( 45 )

for i in range ( 2 ) :

forward ( - 100 )

forward ( 200 )

forward ( - 100 )

left ( 90 )

def line ( ) :

speed ( 10 )

color ( "yellow" )

pensize ( 40 )

forward ( 80 )

backward ( 160 )

def check ( ) :

speed ( 10 )

penup ( )

setposition ( 0 , - 35 )

pendown ( )

color ( "green" )

pensize ( 40 )

left ( 150 )

forward ( 50 )

backward ( 50 )

right ( 100 )

forward ( 150 )

rating = int ( input ( "A number 1-10... " ) )

if rating <= 4 :

ecks ( )

elif rating >= 8 :

check ( )

else :

line ( )

- 2.17.5: Happy/ Sad Face

speed ( 10 )

def happy_face ( ) :

penup ( )

left ( 90 )

backward ( 100 )

right ( 90 )

begin_fill ( )

color ( "yellow" )

circle ( 100 )

end_fill ( )

setposition ( - 80 , 10 )

color ( "black" )

begin_fill ( )

circle ( 10 )

end_fill ( )

forward ( 140 )

begin_fill ( )

circle ( 10 )

end_fill ( )

setposition ( - 30 , - 30 )

pensize ( 8 )

pendown ( )

right ( 90 )

circle ( 20 , 180 )

def sad_face ( ) :

penup ( )

left ( 90 )

backward ( 100 )

right ( 90 )

begin_fill ( )

color ( "yellow" )

circle ( 100 )

end_fill ( )

setposition ( - 80 , 10 )

color ( "black" )

begin_fill ( )

circle ( 10 )

end_fill ( )

forward ( 140 )

begin_fill ( )

circle ( 10 )

end_fill ( )

setposition ( - 10 , - 30 )

pensize ( 8 )

pendown ( )

left ( 90 )

circle ( 20 , 180 )

happy = input ( "Yes or No? " )

if happy == "Yes" :

happy_face ( )

if happy == "No" :

sad_face ( )

- 2.18.4: Increasing Squares

setposition ( 200 , 200 )

setposition ( - 200 , - 200 )

speed ( 0 )

def square_maker ( ) :

for i in range ( 4 ) :

forward ( length )

left ( 90 )

length = 0

while length <= 400 :

square_maker ( )

length = length + 25

- 2.18.5: Guess a Number

def ecks ( ) :

speed ( 10 )

color ( "red" )

pensize ( 40 )

left ( 45 )

for i in range ( 2 ) :

forward ( - 100 )

forward ( 200 )

forward ( - 100 )

left ( 90 )

def check ( ) :

speed ( 10 )

penup ( )

setposition ( 0 , - 35 )

pendown ( )

color ( "green" )

pensize ( 40 )

left ( 150 )

forward ( 50 )

backward ( 50 )

right ( 100 )

forward ( 150 )

user_number = int ( input ( "Guess a number from 1-10 " ) )

if user_number == 8 :

check ( )

if user_number != 8 :

ecks ( )

- 2.19.4: Guess a Number 2.0

def up ( ) :

penup ( )

setposition ( 0 , 0 )

pendown ( )

speed ( 10 )

begin_fill ( )

color ( "orange" )

pensize ( 30 )

left ( 90 )

backward ( 100 )

forward ( 175 )

left ( 135 )

forward ( 100 )

left ( 135 )

forward ( 150 )

left ( 135 )

forward ( 100 )

end_fill ( )

def down ( ) :

left ( 180 )

penup ( )

setposition ( 0 , 0 )

pendown ( )

speed ( 10 )

begin_fill ( )

color ( "blue" )

pensize ( 30 )

left ( 90 )

backward ( 100 )

forward ( 175 )

left ( 135 )

forward ( 100 )

left ( 135 )

forward ( 150 )

left ( 135 )

forward ( 100 )

end_fill ( )

def check ( ) :

penup ( )

setposition ( 0 , 0 )

pendown ( )

speed ( 10 )

penup ( )

setposition ( 0 , - 35 )

pendown ( )

color ( "green" )

pensize ( 40 )

left ( 150 )

forward ( 50 )

backward ( 50 )

right ( 100 )

forward ( 150 )

user_number = int ( input ( "Guess a number from 1-10 " ) )

if user_number == 8 :

check ( )

if user_number > 8 :

down ( )

if user_number < 8 :

up ( )

- 2.19.5: Circle Pyramid 2.0

speed ( 0 )

row_value = 0

radius = 25

def move_to_row ( num_blocks ) :

x_value = - ( ( num_blocks* 50 ) / 2 )

y_value = - 200 + ( 50 *row_value )

penup ( )

setposition ( x_value , y_value )

pendown ( )

def draw_block_row ( num_blocks ) :

for i in range ( num_blocks ) :

circle ( radius )

forward ( 50 )

num_blocks = int ( input ( "Bottom row circles? (8 or less): " ) )

for i in range ( num_blocks ) :

move_to_row ( num_blocks )

row_value = row_value+ 1

draw_block_row ( num_blocks )

num_blocks = num_blocks- 1