简体   繁体   中英

How to draw a star using Turtle

Let's say a function named draw_star takes 2 parameters: size and points, as:

def draw_star(size,points):

And if I were to call the function using

draw_star(100, 9)

the output should be like this在此处输入图片说明

The reference I was given is this:

import turtle as t
for x in range(18):
    t.forward(100)
    if x%2 == 0:
        t.left(175)
    else:
        t.left(225)

now if I call draw_star(100,7) , I want it to draw the same star but with 7 points

What is your question?

If you want to convert the reference to a function you can do it like this:

import turtle as t

def draw_star(size, points):
    for x in range(points):
        t.forward(size)
        if x % 2 == 0:
            t.left(175)
        else:
            t.left(225)

Now you can call the function: draw_star(100, 18)

The reference you were given works and creates an 18 point star

import turtle as t

def draw_star(size, points):
    tipcorner = 5
    cavity = ( 360+points*tipcorner )/points #get corner between tips 
    for x in range(points*2):
        t.forward(size)
        if x % 2 == 0:
            t.left(180-tipcorner)
        else:
            t.right(180-cavity)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM