【问题标题】:Don't understand this AttributeError: module 'turtle' has no attribute 'Turtle' [duplicate]不明白这个AttributeError:模块'turtle'没有属性'Turtle' [重复]
【发布时间】:2019-05-10 14:05:32
【问题描述】:
#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)

代码给出错误信息如下:

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'
>>>

我不明白错误信息。我怎样才能使代码工作?这段代码在大约 3 到 4 个月前曾经顺利运行。

【问题讨论】:

  • 问题解决了。我创建了一个 python 代码并错误地将其命名为“turtle.py”。它保存在与上述代码相同的文件夹中。所以,它干扰了海龟库

标签: python python-3.x turtle-graphics python-turtle


【解决方案1】:

将您的文件名更改为 turtle_something.py 之类的其他名称。因为它与turtle 库冲突。

【讨论】:

  • 这个答案应该是被接受的,恕我直言。
【解决方案2】:

问题已解决。我在创建 python 脚本时犯了一个错误,将其命名为“turtle.py”并将其保存在我保存此代码的同一文件夹中。所以,它干扰了海龟库

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 2018-10-16
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多