Don't understand this AttributeError: module 'turtle' has no attribute 'Turtle' [duplicate]

风格不统一 提交于 2019-12-12 19:14:09

问题


#archimedes spiral by rays

import math
import turtle

def spiral(t, a, b):
    diff=5
    number=500
    for i in range(number):
        t.penup()
        t.fd(a+b*i*diff*math.pi/180)
        t.pendown()
        t.lt(90)
        t.fd(10)
        t.bk(10)
        t.rt(90)
        t.penup()
        t.bk(a+b*i*diff*math.pi/180)
        t.lt(diff)


bob=turtle.Turtle()
bob.speed(1000)

spiral(bob,0, 2)

The code gives an error message as follows:

RESTART: C:\Users\Manish Kumar\Desktop\TBN\repository\Competitive Programming\PYTHON\python scripts\archimedian_spiral.py

Traceback (most recent call last):
  File "C:\Users\Manish Kumar\Desktop\TBN\repository\Competitive Programming\PYTHON\python scripts\archimedian_spiral.py", line 4, in <module>
    import turtle

File "C:\Users\Manish Kumar\Desktop\TBN\repository\Competitive Programming\PYTHON\python scripts\turtle.py", line 7, in <module>
    bob=turtle.Turtle()

AttributeError: module 'turtle' has no attribute 'Turtle'
>>>

I do not understand the error message. How can I make the code work? As this code used to run smoothly some 3 to 4 months back.


回答1:


Are you using an online system (e.g. Trinket or similar)?. I do not get this error when I run your code locally but have had similar problems when trying to set up trinkets for students.

I see you have resolved the issue. Good to know and a useful thing to remember - avoid using existing library names when naming your files.




回答2:


Change your file name with something else like turtle_something.py. Because of its conflicting with the turtle library.




回答3:


The problem is resolved. I made a mistake of creating a python script, naming it "turtle.py" and saving it in the same folder where i had kept this code. So, it was interfering with the turtle library



来源:https://stackoverflow.com/questions/53692691/dont-understand-this-attributeerror-module-turtle-has-no-attribute-turtle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!