# Plz help me finish these last two questions for computer science :(

plz help me finish these last two questions for computer science 🙁 Using Python v2

The Lesson:

Functions & Turtles
There are many ways to do graphics in Python, using the “turtle” module is one of the easiest. Turtle graphics are a fun way to review the use of functions. Load the code below into Wing and see what happens:

# This program will draw an angled line
import turtle

turtle.color(“blue”) # colour will be blue
turtle.up() # pick up the pen

turtle.setx(0) # put the x coordinate of the pen at 0
turtle.sety(0) # put the y coordinate of the pen at 0
turtle.down() # put the pen down

deg = 45
turtle.right(deg) # turn right the specified number of degrees
turtle.forward(200) # move the turtle forward a distance of 200

turtle.up() # pick up the pen
turtle.goto(-150,-120) # go to this coordinate position
turtle.color(“red”) # change the colour
turtle.write(“Done!”) # print the Done message.

You will see that a line is drawn in the “turtle” window.
> setx and sety represent the horizontal and vertical axes
> the turtle.color function can either take 3 arguments (red, green, blue – each a floating point value between the value of 0 and 1 to represent a specific shade of colour) or a value of “red”, “green”, or “blue”.
> For the other turtle functions see the associated comments

What if we want to draw additional lines? This is where the use of functions can be very useful. Rather than retyping the code over and over, we could just pass parameters to a function, and get different colours and line orientations. See the example below:

# This program will draw an angled line
import turtle

def draw_angled_line():

turtle.color(“blue”) # colour will be blue
turtle.up() # pick up the pen

turtle.setx(0) # put the x coordinate of the pen at 0
turtle.sety(0) # put the y coordinate of the pen at 0
turtle.down() # put the pen down

deg = 45
turtle.right(deg) # turn right the specified number of degrees
turtle.forward(200) # move the turtle forward a distance of 200

turtle.up() # pick up the pen
turtle.goto(-150,-120) # go to this coordinate position
turtle.color(“red”) # change the colour
turtle.write(“Done!”) # print the Done message.

draw_angled_line()
What is the problem with this? Let’s fix it!

# This program will draw an angled line
import turtle

def draw_angled_line(x, y):

turtle.color(“blue”) # colour will be blue
turtle.up() # pick up the pen

turtle.setx(x) # put the x coordinate of the pen at 0
turtle.sety(y) # put the y coordinate of the pen at 0
turtle.down() # put the pen down

deg = 45
turtle.right(deg) # turn right the specified number of degrees
turtle.forward(200) # move the turtle forward a distance of 200

turtle.up() # pick up the pen
turtle.goto(-150,-120) # go to this coordinate position
turtle.color(“red”) # change the colour
turtle.write(“Done!”) # print the Done message.

draw_angled_line(0, 0)
draw_angled_line(20,20)

Note how x and y parameters are passed to the function. Recalling this function with diffierent parameters will only require one additional line of code for each line drawn on the screen.

We can make this function easier to write by using one command at the top of your file from turtle import * . This command has the Python interpreter think that all of the turtle function definitions are in your program so it does not have to go out and find the turtle module somewhere else. Here’s how to do it:

# This program will draw an angled line
from turtle import *

def draw_angled_line(x, y, r, g, b):

color(r, g, b) # set the colour
up() # pick up the pen

setx(x) # put the x coordinate of the pen at 0
sety(y) # put the y coordinate of the pen at 0
down() # put the pen down

deg = 45
right(deg) # turn right the specified number of degrees
forward(200) # move the turtle forward a distance of 200

up() # pick up the pen
goto(-150,-120) # go to this coordinate position
color(“red”) # change the colour
write(“Done!”) # print the Done message.

draw_angled_line(0, 0, 0,1,0) # begins at (0,0), colour is green
draw_angled_line(20,20, 0, 0.5, 0.5) # begins at (20,20), colour is a mix of green and blue

Turtle Function Description
color(val) Sets the colour of the pen
up() Lifts the pen up(so no colour shows on the screen)
down() Puts the pen down on the screen
setx(val) Positions the pen’s x position
sety(val) Positions the pen’s y position
right(degree) Turns right the specified number of degrees
forward(val) Move forward a distance specified by val
goto(x) Go to the coordinate specified by (x,y)
write(string) Print out string onto the screen

The Assignments:
3. Using turtle graphics function example above (without the height variable):
a) Create a square by passing the appropriate parameters. Make each side of the square a different color.
b) Create a triangle by passing the appropriate parameters.

4. Using turtle graphics define two functions to create the following shapes:

draw_star(): define a function to draw 8 lines at equally spaced angles in the 4 quadrants of the Cartesian plane (45 degrees apart from each other). (Hint: use a for loop)
draw_square_patterns(): define a function to draw 8 squares shifted at a 45 degree angle from each other. (Hint: use a for loop)