Skip to content
Advertisement

Turtle graphic not running?

i’m more of a backend web developer and this is for a friend of mine but i have this issue with turtle where her code wont run and i keep getting syntax errors saying certain things aren’t defined. Heres the code

    import turtle

    turtle.speed(10)

def blue_circle():
    penup()
    setposition(100,50)
    color("blue")
    begin_fill()
    circle(60)
    end_fill()

def red_circle():
    penup()
    setposition(-100,50)
    color("red")
    begin_fill()
    circle(60,360,4)
    end_fill()

def yellow_half_circle():
    penup()
    setposition(-115,-150)
    color("yellow")
    begin_fill()
    circle(60,180)
    end_fill()

def green_pentagon():
    penup()
    setposition(100,-150)
    left(180)
    color("green")
    begin_fill()
    circle(60,360,5)
    end_fill()

   blue_circle()
   red_circle()
   yellow_half_circle()
   green_pentagon()

Advertisement

Answer

If you use import turtle then you have to use turtle.penup(), turtle.setposition(), etc.

import turtle

turtle.speed(10)

def blue_circle():
    turtle.penup()
    turtle.setposition(100,50)
    turtle.color("blue")
    turtle.begin_fill()
    turtle.circle(60)
    turtle.end_fill()

def red_circle():
    turtle.penup()
    turtle.setposition(-100,50)
    turtle.color("red")
    turtle.begin_fill()
    turtle.circle(60,360,4)
    turtle.end_fill()

def yellow_half_circle():
    turtle.penup()
    turtle.setposition(-115,-150)
    turtle.color("yellow")
    turtle.begin_fill()
    turtle.circle(60,180)
    turtle.end_fill()

def green_pentagon():
    turtle.penup()
    turtle.setposition(100,-150)
    turtle.left(180)
    turtle.color("green")
    turtle.begin_fill()
    turtle.circle(60,360,5)
    turtle.end_fill()

# --- main ---

blue_circle()
red_circle()
yellow_half_circle()
green_pentagon()

turtle.exitonclick()

enter image description here


Original code would work if you would use from turtle import * but import * is not preferred (PEP 8 — Style Guide for Python Code)

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement