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
JavaScript
x
42
42
1
import turtle
2
3
turtle.speed(10)
4
5
def blue_circle():
6
penup()
7
setposition(100,50)
8
color("blue")
9
begin_fill()
10
circle(60)
11
end_fill()
12
13
def red_circle():
14
penup()
15
setposition(-100,50)
16
color("red")
17
begin_fill()
18
circle(60,360,4)
19
end_fill()
20
21
def yellow_half_circle():
22
penup()
23
setposition(-115,-150)
24
color("yellow")
25
begin_fill()
26
circle(60,180)
27
end_fill()
28
29
def green_pentagon():
30
penup()
31
setposition(100,-150)
32
left(180)
33
color("green")
34
begin_fill()
35
circle(60,360,5)
36
end_fill()
37
38
blue_circle()
39
red_circle()
40
yellow_half_circle()
41
green_pentagon()
42
Advertisement
Answer
If you use import turtle
then you have to use turtle.penup()
, turtle.setposition()
, etc.
JavaScript
1
46
46
1
import turtle
2
3
turtle.speed(10)
4
5
def blue_circle():
6
turtle.penup()
7
turtle.setposition(100,50)
8
turtle.color("blue")
9
turtle.begin_fill()
10
turtle.circle(60)
11
turtle.end_fill()
12
13
def red_circle():
14
turtle.penup()
15
turtle.setposition(-100,50)
16
turtle.color("red")
17
turtle.begin_fill()
18
turtle.circle(60,360,4)
19
turtle.end_fill()
20
21
def yellow_half_circle():
22
turtle.penup()
23
turtle.setposition(-115,-150)
24
turtle.color("yellow")
25
turtle.begin_fill()
26
turtle.circle(60,180)
27
turtle.end_fill()
28
29
def green_pentagon():
30
turtle.penup()
31
turtle.setposition(100,-150)
32
turtle.left(180)
33
turtle.color("green")
34
turtle.begin_fill()
35
turtle.circle(60,360,5)
36
turtle.end_fill()
37
38
# --- main ---
39
40
blue_circle()
41
red_circle()
42
yellow_half_circle()
43
green_pentagon()
44
45
turtle.exitonclick()
46
Original code would work if you would use from turtle import *
but import *
is not preferred (PEP 8 — Style Guide for Python Code)