试图用龟画画

时间:2013-05-30 18:16:16

标签: python-3.x drawing

我写了一个简单的代码,应绘制一个正方形,见下文:

    from turtle import Screen,Turtle
    def draw(directions,length,angle,x=0,y=0):
        s = Screen
        t = Turtle
        t.up()
        t.setpos(x,y)
        t.down()
        # iterate over directions
        for move in directions:
            if move =='F':
               t.forward(length)
            elif move == 'L':
               t.lt(angle)
            elif move =='R':
               t.rt(angle)
            else:
               pass

s.exitonclick()

但是我收到一条我不明白的错误消息。见下文

     >>> draw('FLFLFLFL',50,90)
     Traceback (most recent call last):
     File "<pyshell#43>", line 1, in <module>
        draw('FLFLFLFL',50,90)
      File "C:/Documents and Settings/RonnieE/Mina dokument/GradSchool/CSC401/Homework 
      7/test1.py", line 11, in draw
          t.up()
     TypeError: penup() missing 1 required positional argument: 'self'

我做错了什么?

1 个答案:

答案 0 :(得分:0)

此:

    s = Screen
    t = Turtle

应该是:

    s = Screen()
    t = Turtle()

否则,st只是ScreenTurtle类的新名称。调用该类以生成实例

此外,s.exitonclick()应位于draw定义内(与for对齐)。