海龟模式错误

2024-10-02 18:19:01 发布

您现在位置:Python中文网/ 问答频道 /正文

#!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()

这是我的代码,还没有完成。我试着让它在输入的方程上画一条直线,用海龟图形。但是当我运行它时,我得到了这个错误。我能做些什么吗?在

^{pr2}$

Tags: thenoneisasnotcodetimmyturtle
1条回答
网友
1楼 · 发布于 2024-10-02 18:19:01

您需要创建Turtle对象的实例

timmy = Turtle()

注意()调用部分。如果没有调用,timmy上的所有方法都是未绑定的,并且不会自动提供它们的self参数。结果,您最终将320作为self传入,但这不起作用。在

相关问题 更多 >