DeltaFlyer
DeltaFlyer

Reputation: 471

Trying to figure out exercise from How to Think Like Computer Scientist Learning With Python Three with Turtle Module, receiving NameError

I'm trying to work out the first exercise from How to Think Like a Computer Scientist: Learning with Python 3, chapter 4. I am attempting to learn how to integrate functions into code using the turtle module. When I try to run the code below, I receive the following error at line 8:

"NameError: name 't' is not defined"

How do I fix this?

import turtle

def square_array(t, sz):
    """
    Have turtle t make a square.
    """
for i in range (3):
  t.forward(sz)
  t.left(90)

wn= turtle.Screen()
wn.bgcolor("lightgreen")
wn.title("Alex makes some squares dawg")

alex=turtle.Turtle
alex.pensize(5)
alex.color("red")

for i in range (4):
    square_array(alex, 20)
    alex.forward(20)

wn.exitonclick()

Upvotes: 1

Views: 912

Answers (1)

AChampion
AChampion

Reputation: 30288

Just change your indentation:

def square_array(t, sz):
    """
    Have turtle t make a square.
    """
    for i in range(3):
        t.forward(sz)
        t.left(90)

And you need to actually call Turtle to initialise it:

alex = turtle.Turtle()

Upvotes: 3

Related Questions