0

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

    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'

我做错了什么?

4

1 回答 1

0

这个:

    s = Screen
    t = Turtle

应该:

    s = Screen()
    t = Turtle()

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

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

于 2013-05-31T05:31:38.417 回答