乌龟的问题

时间:2016-03-18 17:37:41

标签: python syntax module attributeerror

我收到此错误:

Traceback (most recent call last):
File "C:/Users/jadyn/Documents/file type computer science.py", line 1, in <module>
from turtle import Screen, Turtle
File "C:/Users/jadyn/Documents\turtle.py", line 4, in <module>
t = turtle.Pen()
AttributeError: 'module' object has no attribute 'Pen'

当我尝试运行此

from turtle import Screen, Turtle
from random import randint


def getDistance(routeName, startingPoint, pointA, pointB, pointC, endingPoint):
    global routes

    route = 0
    route += getDistance(startingPoint, pointA)
    route += getDistance(pointA, pointB)
    route += getDistance(pointB, pointC)
    route += getDistance(pointC, endingPoint)
    routes += [[routeName, route, startingPoint, pointA, pointB, pointC, endingPoint]]
points = []

def Distance(pointOne, pointTwo):
    lengthOne = pointOne[0] - pointTwo[0]
    lengthTwo = pointOne[1] - pointTwo[1]

    lengthOneSquared = lengthOne * lengthOne
    lengthTwoSquared = lengthTwo * lengthTwo

    lengthThreeSquared = lengthOneSquared + lengthTwoSquared

    lengthThree = lengthThreeSquared ** 0.5

    return lengthThree


for i in range(5):
    randomOne = randint(-24,25)
    randomTwo = randint(-24,25)

    points += [[randomOne,randomTwo]]

startingPoint = points[randint(0,4)]
points.remove(startingPoint)

endingPoint = points[randint(0,3)]
points.remove(endingPoint)

routes = []

getDistance("Route One", startingPoint, points[0], points[1], points[2], endingPoint)
getDistance("Route Two", startingPoint, points[1], points[2], points[0], endingPoint)
getDistance("Route Three", startingPoint, points[2], points[0], points[1], endingPoint)
getDistance("Route Four", startingPoint, points[0], points[2], points[1], endingPoint)
getDistance("Route Five", startingPoint, points[1], points[0], points[2], endingPoint)
getDistance("Route Six", startingPoint, points[2], points[1], points[0], endingPoint)

routesSorted = sorted(routes,key=lambda l:l[1])

print(routesSorted[0])

scale = 15
window = Screen()
cursor = Turtle()
cursor.penup()
cursor.goto(routesSorted[0][2][0] * scale, routesSorted[0][2][1] * scale)
cursor.pendown()
for i in range(3):
    cursor.goto(routesSorted[0][i+3][0] * scale, routesSorted[0][i+3][1] * scale)
cursor.goto(routesSorted[0][6][0] * scale,routesSorted[0][6][1] * scale)

window.mainloop()   

你能告诉我为什么我已经尝试了几次但仍然不明白为什么会这样!

1 个答案:

答案 0 :(得分:2)

我看到两个可能的答案:

  1. Python名称区分大小写。使用turtle.pen(),而不是turtle.Pen()
  2. 您的本地文件turtle.py与海龟模块冲突。将本地文件重命名为其他文件。