<jupyter_start><jupyter_text>Introduction to Methods Using TurtleHere we introduce you to using Python methods by making a method for drawing a star, then repeatedly calling that method from the main program.  You can copy and paste this code into your IDE to see the result.<jupyter_code># This is a program that will draw stars with random values
# all over the window. The stars are drawn using a method 
# with parameters.

# Program Structure Note:
# imports come first
# methods come next
# main program comes last

import turtle as t
import random

# method to draw a star
def draw_star(size, num_rays, color, x, y):
    # go to the right location
    t.goto(x,y)
    # set the proper color
    if( color % 4 == 0 ):
        t.color("red")
    if( color % 4 == 1 ):
        t.color("green")
    if( color % 4 == 2 ):
        t.color("yellow")
    if( color % 4 == 3 ):
        t.color("blue")

    # draw the star
    t.pendown()
    for i in range(num_rays):     # loop num_rays number of times
        t.left( 360 / num_rays )  # num_rays lines in our star
        t.forward(size)
        t.backward(size)
    t.penup()
    # end of the draw_star method

# main program
t.reset()  # clear the window for drawing
t.shape("turtle")
t.penup()
t.pensize(3)

num_stars = 20

for i in range(num_stars):
    # setup star values
    star_size = random.randrange(10,100)  # ray size from 10 to 100 px
    star_rays = random.randrange(5,10)    # num rays from 5 through 9
    star_color = random.randrange(4)      # there will be 4 possible color values
    star_x = random.randrange(-300, 300)  # set the star's x location
    star_y = random.randrange(-300, 300)  # set the star's y location
    draw_star(star_size, star_rays, star_color, star_x, star_y)
    # end of for loop

t.hideturtle()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup