简体   繁体   中英

Why am i getting a attribute error when its in the right format?

I keep getting a issue where color bassed commands arent working. I have tried variables and (_color) in front of the 'color'. I have never ran into this problem befor.

import turtle

wn = turtle.Screen()
wn.setup(400,400)

ray = turtle.Turtle
ray.pencolor('red')
ray.dot(20)
Traceback (most recent call last):
  File "/home/pi/mu_code/words(1).py", line 7, in <module>
    ray.pencolor('red')
  File "/usr/lib/python3.7/turtle.py", line 2257, in pencolor
    return self._color(self._pencolor)
AttributeError: 'str' object has no attribute '_color'

You forgot to put the parentheses, because of which instead of object of Turtle class, ray is being misassigned.

>>> import turtle
>>> ray = turtle.Turtle
>>> type(ray)
<class 'type'>
>>> ray = turtle.Turtle()
>>> type(ray)
<class 'turtle.Turtle'>

Edit the below line in your code.

ray = turtle.Turtle()

will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM