0

I am attempting to create a basic graphic in Turtle using loops to create a series of

4 yellow boxes that are 160 units on a side w/

4 smaller blue boxes half that size superimposed on the yellow,

4 smaller green boxes half the size of the blue over the blue,

4 white and 4 red in the same fashion.

I am having trouble creating loops to do so. Here is my code thus far, and thank you!:

from turtle import Turtle
t = Turtle()

for i in range(4):
    t.begin_fill ()
    t.fillcolor("yellow")
    t.forward(160)
    t.left(90)
    t.forward(160)
    t.left(90)
    t.forward(160)
    t.left(90)        
    t.forward(160)
    t.end_fill()
    t.hideturtle( )

for i in range(4):
    t.begin_fill ()
    t.fillcolor("blue")
    t.forward(120)
    t.left(90)
    t.forward(120)
    t.left(90)
    t.forward(120)
    t.left(90)
    t.forward(120)
    t.end_fill()
    t.hideturtle( )

for i in range(4):
    t.begin_fill ()
    t.fillcolor("green")
    t.forward(80)
    t.left(90)
    t.forward(80)
    t.left(90)
    t.forward(80)
    t.left(90)
    t.forward(80)
    t.end_fill()
    t.hideturtle( )

for i in range(4):
    t.begin_fill ()
    t.fillcolor("white")
    t.forward(40)
    t.left(90)
    t.forward(40)
    t.left(90)
    t.forward(40)
    t.left(90)
    t.forward(40)
    t.end_fill()
    t.hideturtle( )

for i in range(4):
    t.begin_fill ()
    t.fillcolor("red")
    t.forward(20)
    t.left(90)
    t.forward(20)
    t.left(90)
    t.forward(20)
    t.left(90)
    t.forward(20)
    t.end_fill()
    t.hideturtle( )
  • What is the problem exactly? The code seems to be working? The only suggestion I have is that you could use another loop to actually draw the square. ie, do the "forward, then left" four times. – nakedfanatic Oct 06 '14 at 01:04
  • Right, however I am looking for a way to compact this code into appx 25 or so lines. I would like to know if there is a way to execute this code without having to enter each individual square in its own function. In other words, is there a way to enter all sizes in a function and all fill colors in another function and then execute the code with only one t.begin_fill, t.fillcolor, t.forward, t.left...ect? – Python Novice Oct 06 '14 at 01:45
  • Further clarification: – Python Novice Oct 06 '14 at 01:46
  • I would like to write this w/ a main function and box function to use repetition to allow me to compact this into less than 25 to 30 lines? And thank you for responding! – Python Novice Oct 06 '14 at 01:48

2 Answers2

0

since the only movement is three times (forward, left) and 1 time (forward, right) you could start condensing the code by encapsulating this basic (forward - left) action into a function. E.g.

def fw_lft():
    t.begin_fill()
    t.fillcolor()
    t.forward(160)
    t.left(90)

Then you can call this function inside a function that repeats the input function twice. E.g.

def twice(f):
    f()
    f()

Another function can draw a full square by repeating twice 2 times and adding one right movement:

def square():
    twice(fw_lft)
    twice(fw_lft)
    t.right(90)

And the last one you repeat the square drawing twice:

def four_squares():
    twice(square)
    twice(square)

When you call this last function you have the first four squares. This is the basic concept of condensing code (=encapsulating into functions) that you then can control the flow of in loops etc..

---It would take me some more studying to get the color input defined in a global way, so that you can have four_squares(c=color), but this post has a some example info on it: Help Defining Global Names

Community
  • 1
  • 1
oaklander114
  • 3,143
  • 3
  • 16
  • 24
0

This sounds like a homework problem, so I don't want to give too much away.

What you need is to write one box function that can draw boxes of any size, and of any colour. It might look something like this:

def myBoxFunction(size, color):
    # draw a box of the right size, and the right color.
    # hint: use a loop to draw the four sides.

Then you can "call" the function with different parameters, as many times as you need, like this:

myBoxFunction(160, "yellow")
myBoxFunction(80, "blue") # for example

You can save more code by using a loop to draw each of the four boxes needed at each size.

You could save even more code by looping over the collection of sizes and colors that you need, like this:

for size, color in ((160, "yellow"), (120, "blue")):
    myBoxFunction(size, color) # only draws one box, but you get the idea.
nakedfanatic
  • 3,108
  • 2
  • 28
  • 33