K Mac
K Mac

Reputation: 1

I have some problem with turtle color script

I'm follow a little turtle project from youtube.And i have copy the same code from the video. When i run the code it come out:

Traceback (most recent call last):
File "Tur1.py", line 7, in <module>
JJ.color('red', 'blue')
File "C:\Python\Python37\lib\turtle.py", line 2218, in color
pcolor = self._colorstr(pcolor)
AttributeError: 'str' object has no attribute '_colorstr

This is my Code:

import turtle
import random
JJ = turtle.Turtle 
colors = ['red', 'blue','green', 'purple', 'yellow', 
'orange','black']
JJ.color('red', 'blue')

Upvotes: 0

Views: 57

Answers (1)

Spencer D
Spencer D

Reputation: 3486

It seems that your code is missing the parentheses on the end of its assignment to JJ. As a result, JJ just contains a reference to the Turtle class in the turtle module, but does not contain an actual instance of the Turtle class (i.e., an instantiated object). (My apologies if you are new to python, in which case I suspect that my explanation might not make a lot of sense to you.)

Short answer/fix, simply rewrite your code as follows:

import turtle
import random
JJ = turtle.Turtle()
colors = ['red', 'blue', 'green', 'purple', 'yellow', 'orange', 'black']
JJ.color('red', 'blue')

Note that JJ = turtle.Turtle has changed to JJ = turtle.Turtle() which executes the methods necessary to initialize a Turtle object.

Upvotes: 1

Related Questions