2014-09-29 85 views
1
#!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' 

回答

3

您需要創建一個實例Turtle對象

timmy = Turtle() 

注意()調用一部分。如果沒有呼叫,timmy上的所有方法都是未綁定及其self參數未自動提供。結果,你最終通過320作爲self,並且不起作用。

+0

謝謝!它現在有效,將來我一定會記住這一點。 – SonoFratello 2014-09-29 11:39:29