I just started using turtle and I found code online for Sierpinski's Carpet. It should be able to draw the Carpet without problem, but I don't know how to add a display variable. I just found this code online to help me. By the way, this is not homework. It is a fun little project to see the carpet in action. Here's the code
import turtle,math
def s(n, l):
if n == 0: # stop conditions
# draw filled rectangle
turtle.color('black')
turtle.begin_fill()
for _ in range (4):
turtle.forward(l)
turtle.left(90)
turtle.end_fill()
else: # recursion
# around center point create 8 smalles rectangles.
# create two rectangles on every side
# so you have to repeat it four times
for _ in range(4):
# first rectangle
s(n-1, l/3)
turtle.forward(l/3)
# second rectangle
s(n-1, l/3)
turtle.forward(l/3)
# go to next corner
turtle.forward(l/3)
turtle.left(90)
# update screen
turtle.update()
# --- main ---
# stop updating screen (to make it faster)
turtle.tracer(0)
# start
s(4, 400)
# event loop
turtle.done()
I get a very weird error
Traceback (most recent call last):
File "main.py", line 40, in <module>
turtle.tracer(0)
File "<string>", line 6, in tracer
File "/usr/local/lib/python3.8/turtle.py", line 3662, in Screen
Turtle._screen = _Screen()
File "/usr/local/lib/python3.8/turtle.py", line 3678, in __init__
_Screen._root = self._root = _Root()
File "/usr/local/lib/python3.8/turtle.py", line 434, in __init__
TK.Tk.__init__(self)
File "/usr/local/lib/python3.8/tkinter/__init__.py", line 2261, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
This is not a duplicate because Iām asking how to fix THIS CODE, not how the error works.