paDrEdadash
paDrEdadash

Reputation: 19

AttributeError: 'NoneType' object has no attribute 'pencolor'

Hi everyone.

It's my first time here and I'm new in the Python.

When I wrote this code

import turtle
t=turtle.Pen()
t=turtle.bgcolor("black")
sides=6
colors=("blue", "red", "green", "white", "yellow", "purple")
for x in range(360):
t.pencolor(colors[x % sides])
t.forward(x*3/sides+x)
t.left(360/sides+1)
t.width(x*sides/200)

and ran it, I received a message:

"Traceback (most recent call last):
File "C:/Users/emin_/PycharmProjects/firstproject/AydA.py", line 10, in t.pencolor(colors[x % sides]) AttributeError: 'NoneType' object has no attribute 'pencolor'".

I will be very thankful for any advice and help.

Sincerely, paDrEdadash

Upvotes: 1

Views: 1093

Answers (1)

cdlane
cdlane

Reputation: 41872

Along with the assignment of None in t=turtle.bgcolor("black") that @JohnGordon points out (although turtle.bgcolor("black") is fine), your indentation as shown is incorrect and the code has the potential to error with an index out of range on colors if sides and the len(colors) don't coincidentally match. I recommend an approach like the following to avoid problems:

from turtle import Screen, Turtle

SIDES = 6

COLORS = ("blue", "red", "green", "white", "yellow", "purple")

screen = Screen()
screen.bgcolor("black")

turtle = Turtle()

for x in range(360):
    turtle.pencolor(COLORS[(x % SIDES) % len(COLORS)])
    turtle.forward(x*3 / SIDES + x)
    turtle.left(360 / SIDES+1)
    turtle.width(x * SIDES/200)

screen.exitonclick()

enter image description here

Upvotes: 0

Related Questions