在vscode中如何对tutle函数进行封装

import turtle

bob = turtle.Turtle()

def square(t):

i = 0

while i <5:

    i += 1

    t.fd = 99

    t.lt = 90

    turtle.done()

square(bob)
代码如上,请问为什么我运行后并没有用Tutle画出正方形呢?谢谢各位

Jason990420
最佳答案

有三个问题:

  1. turtle 的 fd, lt 是方法, 不是属性
  2. while i<5: 会画五边, 不是四边
  3. turtle.done() 应该是画完四边形后, 再进入事件回路, 所以不该放在 while loop 中

正确代码为

import turtle


def square(t):
    i = 0
    while i < 4:
        i += 1
        t.fd(99)
        t.lt(90)
    turtle.done()


bob = turtle.Turtle()
square(bob)
3年前 评论
for_future (楼主) 3年前
讨论数量: 1
Jason990420

有三个问题:

  1. turtle 的 fd, lt 是方法, 不是属性
  2. while i<5: 会画五边, 不是四边
  3. turtle.done() 应该是画完四边形后, 再进入事件回路, 所以不该放在 while loop 中

正确代码为

import turtle


def square(t):
    i = 0
    while i < 4:
        i += 1
        t.fd(99)
        t.lt(90)
    turtle.done()


bob = turtle.Turtle()
square(bob)
3年前 评论
for_future (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!