Unit 8 Flashcards

1
Q

What is the difference in writing the code when we learned about the turtle module in a previous unit and drawing with loops?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can we use for loop when drawing with loops?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Draw a triangle using for loop.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Draw a pentagon using for loop.

A

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 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Draw an octagon using for loop.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Using functions can _____ when it comes to drawing with the turtle.

A

Save a lot of time
Make drawing more interactive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a program that draws a shape of any number of sides.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you control our colour options even more?

A

By adding specific values to our code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What other ways other there to control the colour of the turtle?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you make a blue triangle on turtle module?

A

Set red and green to 0 and blue to 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a program that uses RGB to draw a triangle filled with blue color that is 120 pixels large.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What code sets shape_pen’s color to purple (half blue and half red)?

A

shape_pen.color(0.5,0,0.5)
Remember that the format for using color() is color(RED_VALUE, GREEN_VALUE, BLUE__VALUE)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do we make spirals using the turtle module?

A

Loops and functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Write a program that creates a triangle spiral.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Using a function, create a design of spiralling lines in an octagon shape.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Code a program that draws 12 rotating equilateral triangle

A
20
Q

What are some things to keep in mind when debugging your spiraling graphics program?

A

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.

21
Q

Using a function, create a rotating octagon design.

A

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)

22
Q

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()

A

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.

23
Q

Do you know how to make your pen the color white?

A

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.

24
Q

Do you know how to make your pen colour black?

A

color (0, 0, 0) is the color black.
Remember that by default, a turtle Pen object’s line color is black.

25
Q

The following program uses a function to draw a triangle of the user’s desired color and size.
What is wrong with the code?
Here’s the code:

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 = int(input(“How much red should the triangle have (on a scale of 0 to 1)?”))
green = int(input(“How much green should the triangle have (on a scale of 0 to 1)? “ ))
blue = int(input(“How much blue should the triangle have (on a scale of 0 to 1)? “))
draw_triangle(triangle_size, red, green, blue)

A

Another common mistake is forgetting to format user input appropriately before using it in you program.

Try copying and running this code in Python so you can follow along. Run the program using an integer for the side length and decimal values (0.5) for red, green and blue. What happened? Did you get an error?

Traceback (most recent call last):
File “C:\Users\96C32572223744\Desktop\test.py”, line 13, in <module>
red = int(input("How much red should the triangle have (on a scale of 0 to 1)?"))
ValueError: invalid literal for int() with base 10: '0.5'
Python throws a ValueError because the line "red = int(input("How much red should the triangle have....")) we are trying to convert the user's decimal input into integer. Since the program asks the user to input a value between 0 and 1 for the red, green and blue variables, we should be converting this input into a float value. Here's the corrected program:</module>

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 green should the triangle have (on a scale of 0 to 1)? “ ))
blue = float(input(“How much blue should the triangle have (on a scale of 0 to 1)? “))
draw_triangle(triangle_size, red, green, blue)
Be sure to copy and run this corrected code into IDLE to make sure you understand what’s going on here.

*TIP: When processing RGB values from user input, remember to convert decimal inputs into float and not integer.

26
Q

So what are the stuff that you need to take note of for unit 8?

A

Review the debugging tutorials for earlier units on Turtle, Functions, and Loops.
Make sure that the RGB values in the color function are between 0 and 1.
Remember to convert user input to appropriate data types according to the requirements.

27
Q

Fill in the blank with the appropriate value to draw an octagon (eight sides) using a for loop:

import turtle
my_pen = turtle.Pen()
for i in range(1, ______):
my_pen.forward(60)
my_pen.right(360/8)
Question 1Select one:

a.
6

b.
7

c.
8

d.
9

A

d

octagon–> draw eight sides–> (1,9)–> means the 9 is excluded thus it will be drawn eight times

28
Q

RGB stands for _______, ________, _________.

Question 2Select one:

a.
Red, Gold, Blue

b.
Red, Green, Blue

c.
Red, Gray, Black

d.
Ruby, Green, Brown

A

b

29
Q

Fill in the blanks with the correct RGB color combination to draw a triangle filled with the color black:

triangle.fillcolor( \_\_\_\_\_ , \_\_\_\_\_ , \_\_\_\_\_\_ ) Question 3Select one:

a.
0, 0, 0

b.
1, 1, 1

c.
128, 128, 128

d.
“black”, “black”, “black”

A

a

30
Q

The following program does not display the drawing. Which of the following line(s) of code need to be changed to fix the program?

*Choose all that apply.

def my_triangle(side):
triangle.begin_fill()

for count in range(0, 3):
    triangle.forward(side)
    triangle.left(120)
    triangle.end_fill()

import turtle
triangle = turtle.Pen()
triangle_side = 70
triangle.color(1, 1, 1)
my_triangle(triangle_side)
Question 4Select one or more:

a.
def my_triangle(side):

b.
triangle.begin_fill()

c.
for count in range(0, 3):

d.
triangle.forward(side)

e.
triangle.left(120)

f.
triangle.end_fill()

g.
import turtle

h.
triangle = turtle.Pen()

i.
triangle_side = 70

j.
triangle.color(1,1,1)

k.
my_triangle(triangle_side)

A

j

This question requires you to read the instructions carefully. The question is asking why the it is not displaying anything so the only necessary code to change is triangle.color(1,1,1) because this code makes the turtle.Pen() white color which makes it invisible.

31
Q

What shape will this code snippet produce?

import turtle
my_shape = turtle.Pen()

def draw_square(length):
for count in range(0, 8):
my_shape.left(45)
my_shape.forward(length)
my_shape.left(90)

shape_size = 75
draw_square(shape_size)
Question 5Select one:

a.
triangle

b.
square

c.
octagon

d.
8-pointed star

A

d

32
Q

What shape will this code snippet produce?

import turtle
my_shape = turtle.Pen()
def draw_square(length):
for count in range(0, 40):
my_shape.forward(length)
my_shape.left(90)
length = length + 5

shape_size = 5
draw_square(shape_size)
Question 6Select one:

a.
spiral square

b.
spiral octagon

c.
spiral triangle

d.
spiral pentagon

A

a

33
Q

What color will the square be filled with in the following program?

import turtle

def draw_square(size, filled, fill_color):
if filled == True:
t.fillcolor(fill_color)
t.begin_fill()
for x in range(1, 5):
t.forward(size)
t.left(90)
if filled == True:
t.end_fill()

t = turtle.Pen()
draw_square(50, True, (1, 1, 0))
Question 7Select one:

a.
nothing (the square will not be filled)

b.
yellow

c.
green

d.
red

A

b

34
Q

How many different shades of RGB color can be created using the turtle color() function?

Question 8Select one:

a.
3

b.
8

c.
64

d.
infinite

A

d

35
Q

Fill in the blank with the appropriate angle value to draw an octagon (eight sides):

import turtle
t = turtle.Pen()
for i in range(0, 8):
t.forward(60)
t.right(_______)
Question 9Select one:

a.
120

b.
90

c.
60

d.
45

A

d

36
Q

Fill in the blank with all of the correct options that would result in a random-colored line:

*Choose all that apply.

import turtle
import random

def get_random_color():
return ____________

line = turtle.Pen()
red = get_random_color()
green = get_random_color()
blue = get_random_color()

line.color(red, green, blue)
line.forward(100)
Question 10Select one or more:

a.
random.randint(0, 100) / 100

b.
random.random()

c.
random.random(0.0, 1.0)

d.
random.randint() / 100

e.
random.uniform(0, 1)

f.
random.choice([“red”, “green”, “blue”])

g.
None of these choices would work.

A

a,b,e

37
Q

According to best coding practices, which of the following programs is the best choice to draw a square?

Question 1Select one:

a.
import turtle
square = turtle.Pen()

def draw_square(length):
for count in range(0, 4):
square.forward(length)
square.left(90)

draw_square(int(input(“How long should each side of the square be? “)))

b.
import turtle
square = turtle.Pen()

def draw_square(length):
i = 0
while i < 4:
square.forward(length)
square.left(90)
i = i + 1

draw_square(int(input(“How long should each side of the square be? “)))

c.
import turtle
square = turtle.Pen()
square_size = int(input(“How long should each side of the square be? “))
square.forward(square_size)
square.left(90)
square.forward(square_size)
square.left(90)
square.forward(square_size)
square.left(90)
square.forward(square_size)

d.
import turtle
square = turtle.Pen()
square_size = int(input(“How long should each side of the square be? “))
for count in range(0, 4):
square.forward(square_size)
square.left(90)

A

a

38
Q

Fill in the blanks below with the correct RGB color combination to draw a triangle with green lines:

import turtle
triangle = turtle.Pen()
triangle.color(____, ____, ____)
for i in range(0, 3):
triangle.forward(90)
triangle.right(360/3)
Question 2Select one:

a.
1, 0, 1

b.
0, 1, 0

c.
1, 1, 0

d.
0, 1, 1

A

b

39
Q

Fill in the blanks with the correct RGB color combination to draw a triangle filled with the color white:

triangle.fillcolor( \_\_\_\_\_ , \_\_\_\_\_ , \_\_\_\_\_\_ ) Question 3Select one:

a.
0, 0, 0

b.
1, 1, 1

c.
128, 128, 128

d.
“white”, “white”, “white”

A

b

40
Q

Fill in the blanks below to create a program that draws a shape of any number of sides and any length:

import turtle
def draw_shape(sides, side_length):
for x in range(________, sides):
t.forward(side_length)
t.left(360 / ________)

t = turtle.Pen()
sides = int(input(“How many sides? “))
side_length = int(input(“What is the length of each side? “))
draw_shape(sides, side_length)
Question 4Select one:

a.
1 , x

b.
0 , sides

c.
1 , (sides - 2 * 180)

d.
0 , x

A

b

41
Q

In the turtle color() function, RGB color numbers can range from ____________.

Question 5Select one:

a.
0 to 1

b.
0 to 25

c.
-1 to 1

d.
-254 to 255

A

a

42
Q

What shape does the following program produce?

import turtle
my_pen = turtle.Pen()
for i in range(3, 8):
my_pen.forward(60)
my_pen.right(360/5)
Question 6Select one:

a.
a triangle

b.
a square

c.
a pentagon

d.
a hexagon

A

c

43
Q

Describe the circle that will be drawn on the canvas in this program:

import turtle
import random

def draw_circle(r, g, b, fill, rad):
my_shape.color(r, g, b)
my_shape.fillcolor(fill)
my_shape.begin_fill()
my_shape.circle(rad)
my_shape.end_fill()

my_shape = turtle.Pen()
my_shape.pensize(5)
rand = random.randint(10, 100)
draw_circle(0, 0, 1, “yellow”, rand)
Question 7Select one:

a.
a random-sized circle with a red border and yellow fill

b.
a random-sized circle with a green border and yellow fill

c.
a random-sized circle with a yellow border and blue fill

d.
random-sized circle with a blue border and yellow fill

A

d

44
Q

Fill in the blank with the appropriate value to draw a hexagon (six sides) using a for loop:

import turtle
my_pen = turtle.Pen()
for i in range(1, ______):
my_pen.forward(60)
my_pen.right(360/6)
Question 8Select one:

a.
5

b.
6

c.
7

d.
8

A

c

45
Q

What color will the square be filled with in the following program?

import turtle

def draw_square(size, filled, fill_color):
if filled == True:
t.fillcolor(fill_color)
t.begin_fill()
for x in range(1, 5):
t.forward(size)
t.left(90)
if filled == True:
t.end_fill()

t = turtle.Pen()
draw_square(50, False, (1, 1, 0))
Question 9Select one:

a.
red

b.
yellow

c.
green

d.
nothing (the square will not be filled)

A

d

46
Q

Fill in the blanks below to create a program that draws a shape of any number of sides and any length.

________ turtle
def draw_shape(sides, side_length):
for x in range(0, ________):
t.forward(side_length)
t.left(360 / sides)

t = turtle.Pen()
sides = int(input(“How many sides? “))
side_length = int(input(“What is the length of each side? “))
draw_shape(sides, ________)
Question 10Select one:

a.
import, len(str(sides)), side_length

b.
module, sides, sides

c.
import , sides , side_length

d.
import , side_length , sides

A

c