1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Turtle graphics using a function

Discussão em 'Python' iniciado por Stack, Setembro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have this code below and I am trying to draw a colored square. The code works fine. I am curious about one thing though: why can't I define some of the attributes (see those that I commented out)under the function drawColoredSq()? Python doesn't run them if I do this. It only works when I define them after creating the actual turtle (turt in this case) and you can see this in the main() function. Why is this the case? Thank you!!!

    import turtle
    def drawColoredSq(t, sz):
    t.begin_fill()
    for side in ("side A", "side B", "side C", "side D"):
    t.forward(sz)
    t.write(side)
    t.left(90)
    t.end_fill()
    # t.shape("classic")
    # t.pensize(3)
    # t.pencolor("green")
    # t.fillcolor("blue")

    def main():

    wn = turtle.Screen()
    turt = turtle.Turtle()
    wn.bgcolor("maroon")
    wn.setworldcoordinates(0,0,50,50)
    turt.shape("classic")
    turt.pensize(3)
    turt.pencolor("green")
    turt.fillcolor("orange")
    drawColoredSq(turt, 49)
    wn.exitonclick()
    if __name__ == "__main__":
    main()

    Continue reading...

Compartilhe esta Página