Search code examples
pythonvisual-studio-codeturtle-graphicspython-3.9

Turtle graphics window does not appear vscode


I am using vscode. Only a simple turtle statement can show the graphic window effect. Why can't the graphics windows be displayed?

from turtle import *

def star(sidelength):
    '''Draws a 5-pointed star of a
    given sidelength'''
    for i in range(5):
        forward(sidelength)
        right(144) #why this angle??

def starSpiral():
    '''Draws a spiral of stars'''
    length = 5
    for i in range(60):
        star(length)
        right(5)
        length += 5

After running, nothing came out.


Solution

  • According to the information you provided, I ran it on my computer and modified the code:

    1.Draws a 5-pointed star of a given sidelength:

    For drawing a five-pointed star with a given side length, we need to give it an input value:

    import turtle
    def star():
        sidelength = int(input("Please input sidelength:"))
        turtle.begin_fill()
        i = 1
        for i in range(5):
            turtle.forward(sidelength)
            turtle.right(144)
            i += 1
    turtle.end_fill()
    star()
    

    Run:

    enter image description here

    1. Draws a spiral of stars:
    import turtle   
    def starSpiral():
        '''Draws a spiral of stars'''
        for i in range(60):
            turtle.forward(5*i)
            turtle.left(144)
    starSpiral()
    

    run:

    enter image description here

    1. About "right(144) #why this angle?":

      It means to turn 144 degrees to the right, because each angle of the five-pointed star is 36 degrees, so the angle we need to rotate when drawing is 180-36=144 degree.