Unit 8 Flashcards
(46 cards)
What is the difference in writing the code when we learned about the turtle module in a previous unit and drawing with loops?
We had to write the same lines of code several times in order to draw shapes when we learned about the turtle module in a previous unit. But drawing with loops would make drawing with our turtle much simpler.
Can we use for loop when drawing with loops?
Yes. That way, we don’t have to write quite as much code. This also helps prevent typos and it’s a lot more efficient.
Draw a triangle using for loop.
Please ignore the capital letters of the first letter. It is suppose to be small letters.
Import turtle
Triangle = turtle.Pen()
For count in range(0,3):
Triangle.forward(50)
Triangle.left(120)
Draw a pentagon using for loop.
Please ignore the capital letters of the first letter. It is suppose to be small letters.
Import turtle
Pentagon = turtle.Pen()
For count in range(0,5):
pentagon.forward(50)
Pentagon.left(72 )
Draw an octagon using for loop.
Please ignore the capital letters of the first letter. It is suppose to be small letters.
#Create the pen object
Import turtle
Shape = turtle.Pen()
#Loop to draw the octagon
For count in range (0,8):
Shape.forward(60)
Shape.left(360/8)
Using functions can _____ when it comes to drawing with the turtle.
Save a lot of time
Make drawing more interactive
Write a function that draws a triangle of a particular size and then a program that asks the user how big of a triangle they would like to see.
Now, we want to turn this into a function that takes a parameter for the length and then uses that to draw the triangle.
Please ignore the capital letters of the first letter. It is suppose to be small letters.
Import turtle
Triangle = turtle.Pen()
Def draw_triangle(length):
For count in range(0,3):
Triangle.forward(length)
Triangle.left(120)
Triangle_size = int(input(“How long should each side of the triangle be?”))
Draw_triangle(triangle_size)
Write a program that draws a shape of any number of sides.
Please ignore the capital letters of the first letter. It is suppose to be small letters.
Import turtle
Shape = turtle.Pen()
Sides = int(input(“How many sides should the shape have?”))
Size = int(input(“How long should each side be?”))
Def draw_shape(length, number_of_sides):
For count in range(0,3):
Shape.forward(length)
Shape.left(360/ number_of_sides)
Draw_shape(size, sides)
Draw an octagon with a function by first asking the user how long they would like each side of the octagon to be. Then, write a function to draw an octagon that has 8 sides, each of the user’s specified length, in pixels.
Please ignore the capital letters of the first letter. It is suppose to be small letters.
#Create the pen object
Import turtle
Shape = turtle.Pen()
#Function to draw the octagon
Def draw_shape(length, num):
For count in range(0, num):
Shape.forward(length)
Shape.left(360/num)
#Main program
#Prompt the user to enter side length
Shape_side = int(input(“How long should each side be?”))
Draw_shape(shape_side, 8)
How do you control our colour options even more?
By adding specific values to our code.
What other ways other there to control the colour of the turtle?
RGB values where R stands for red, G stands for green, B stands for blue. These values tell you how much red, how much green, and how much blue are in your particular colour (on a scale of 0 to 1, which matches up to 0% of a colour to 100% of a colour).
How do you make a blue triangle on turtle module?
Set red and green to 0 and blue to 1.
What does this code mean?
Please ignore the capital letters of the first letter. It is suppose to be small letters.
Import turtle
Triangle = turtle.Pen()
Def draw_triangle(length):
Triangle.color(0,0,1)
Triangle.begin_fill()
For count in range(0,3):
Triangle.forward(length)
Triangle.left(120)
Triangle.end_fill()
Triangle_size = int(input(“How long should each side of the triangle be?”))
Draw_triangle(triangle_size)
This line: triangle.color(0,0,1) is where we set the color to blue.
This line: triangle.begin_fill() is where we tell our turtle to start filling in with color.
This line: triangle.end_fill() is where we tell our turtle to stop filling it in. Until we write that line, the turtle won’t fill in the shape with color.
Write a program that uses RGB to draw a triangle filled with blue color that is 120 pixels large.
Please ignore the capital letters of the first letter. It is suppose to be small letters.
Import turtle
Triangle = turtle.Pen()
Def draw_triangle(length, red_value, green_value, blue_value):
Triangle.color(red_value, green_value, blue_value)
Triangle.begin_fill()
For count in range(0,3):
Triangle.forward(length)
Triangle.left(120)
Triangle.end_fill()
Triangle_size = int(input(“How long should each side of the triangle be?”))
Red = float(input(“How much red should the triangle have (on a scale of 0 to 1)?”))
Green = float(input(“How much red should the triangle have (on a scale of 0 to 1)?”))
Blue = float(input(“How much red should the triangle have (on a scale of 0 to 1)?”))
Draw_triangle(triangle_size, red, green, blue)
What code sets shape_pen’s color to purple (half blue and half red)?
shape_pen.color(0.5,0,0.5)
Remember that the format for using color() is color(RED_VALUE, GREEN_VALUE, BLUE__VALUE)
How do we make spirals using the turtle module?
Loops and functions.
Write a program that creates a triangle spiral.
Please ignore the capital letters of the first letter. It is suppose to be small letters.
Import turtle
Triangle = turtle.Pen()
Def draw_triangle_spiral(number_of_lines):
For count in range (0, number_of_lines):
Triangle.forward(count)
Triangle.left(120)
Lines = int(input(“How many lines should be in our triangle spiral?”))
Draw_triangle_spiral(lines)
Using a function, create a design of spiralling lines in an octagon shape.
Please ignore the capital letters of the first letter. It is suppose to be small letters.
#Create the pen object
Import turtle
Shape = turtle.Pen()
#Function to draw spiralling lines graphics
Def draw_spiral_shape(num_sides):
For count in range (0,60):
Shape.forward(count)
Shape.left(360/num_sides)
#Main program
Draw_spiral_shape(8)
Code a program that draws 12 rotating equilateral triangle
What are some things to keep in mind when debugging your spiraling graphics program?
There are nested loops. Double check your line indentation for each loop.
The rotating angle in the outer loop is 30 for this particular shape.
A triangle is being drawn in the inner loop.
The triangle side length is 30.
If the shape is not what you want, then check the angle inside the inner loop.
If the program is creating only one shape, i.e. a triangle overwriting its side, then the outer loop is missing or you are not changing the angle to rotate the shape inside the outer loop.
Using a function, create a rotating octagon design.
Create the pen object
import turtle
shape = turtle.Pen()
# Function to draw the rotating shape graphic
def draw_rotating_shape(length, num_sides):
# Outer loop to rotate the angle at the same point
for counter in range (0,20):
# The rotation for the graphic
shape.left(20)
# Inner loop that draws an octagon
for count in range (0, num_sides):
shape.forward(length)
shape.left(360/ num_sides)
# Main program
draw_rotating_shape(30,8)
What is wrong with this code?
import turtle
triangle = turtle.Pen()
def draw_triangle():
triangle.color(2, 0, 1)
triangle.begin_fill()
for count in range(0, 3):
triangle.forward(30)
triangle.left(120)
triangle.end_fill()
draw_triangle()
common error that occurs while working with colors in turtle–> a bad color sequence error.
Try running the above code in IDLE and see what happens. Did you get a bad color sequence error like this?
turtle.TurtleGraphicsError: bad color sequence: (2, 0, 1)
What is Python trying to tell us here with this error message? When using the turtle color() method, the RGB color codes for Red, Green, and Blue range from 0 to 1, inclusive. That is, a color code must be greater than or equal to 0 and less than or equal to 1. Let’s look at our code with the error again: triangle.color(2, 0, 1). The error occurred because ‘2’ is the color value for the Red and not within the valid range.
*TIP: Remember to use RGB values only between 0 and 1 inclusive.
Do you know how to make your pen the color white?
Using color (1, 1, 1) is the color white
If we draw something using a white pen over a white background, we will see nothing on the screen. It may appear that the code is not doing anything, but it is - you just can’t see it.
Do you know how to make your pen colour black?
color (0, 0, 0) is the color black.
Remember that by default, a turtle Pen object’s line color is black.