我收到此错误:
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()
你能告诉我为什么我已经尝试了几次但仍然不明白为什么会这样!
答案 0 :(得分:2)
我看到两个可能的答案:
turtle.pen()
,而不是turtle.Pen()
。turtle.py
与海龟模块冲突。将本地文件重命名为其他文件。