海龟模块出错

时间:2014-09-29 10:23:13

标签: python turtle-graphics

#!usr/bin/env python
from turtle import Turtle
timmy = Turtle #A turtle called Timmy. 

pointA = None
pointB = None
backgroundColour = (255,255,255)
penColour = (0,0,0) #So I can just say penColour, as opposed to (0,0,0)
graphicsWindowHeight = 640
graphicsWindowWidth = 640
equation = str(input("What is the equation as y=mx+c?Do not use spaces, please, it messes up the            code"))
if len(equation) != 6:
    print("You have broken my code. It is not perfect") #Short explanation

#(equation[3]) is gradient, and (equation[5]) is y-intercept, and x-intercept is equation[5] / equation[3]

def calculateAndDrawLine(pointA, pointB):
    #CALCULATE WHAT TO DRAW
    #y = 10 here
    pointA = ((equation[3]/-10)+10)*32,0
    #y = -10 here
    pointB = ((equation[3]/-10)*-1)+10*32,640
    #DRAW IT
    timmy.pencolor(penColour)
    timmy.penwidth(10)
    timmy.pendown()
    timmy.setpos(pointA[0],pointA[1])
    timmy.seth(pointB[0],pointB[1])


def drawAxis():
    timmy.setpos(320,-640)
    timmy.pencolor(penColour)
    timmy.penwidth(10)
    timmy.seth(320,0)
    timmy.setpos(320,0)
    timmy.seth(320,-320)

drawAxis()
calculateAndDrawLine()

这是我的代码,但尚未完成。我试图用它绘制输入方程式的线,使用龟图形。但是当我运行它时,我得到了这个错误。我有什么可以做的吗?

Traceback (most recent call last):
  File "C:\Users\Adam Powell\Documents\Python\Line drawer.py", line 34, in <module>
drawAxis()
  File "C:\Users\Adam Powell\Documents\Python\Line drawer.py", line 27, in drawAxis
timmy.setpos(320,-640)
  File "C:\Python34\lib\turtle.py", line 1773, in goto
    self._goto(Vec2D(*x))
AttributeError: 'int' object has no attribute '_goto'

1 个答案:

答案 0 :(得分:3)

您需要创建Turtle对象的实例

timmy = Turtle()

请注意()来电部分。如果没有通话,timmy上的所有方法都未绑定,并且不会自动提供其self参数。因此,您最终将320作为self传递,但这并不起作用。