Python Forum
Tracy the turtle code shortening - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Tracy the turtle code shortening (/thread-29499.html)



Tracy the turtle code shortening - invaneri - Sep-06-2020

Hey guys,
Im new to the python 3 coding
Im taking a course and I'm trying to go ahead and go forward. I went ahead of a few chapters, but now the assignment requires me to do as follows

"""
Have Tracy draw a sidewalk around the perimeter of the canvas.
The sidewalk should consist of:

8 squares along each side of the canvas
Hints:

Use Top Down Design to break this problem down into smaller problems before writing any code
Determine how long each square’s side should be to fit 8 from edge to edge. Remember, the entire width of the canvas is 400 pixels!
This entire problem can be solved with 16 lines of code, so determine which parts of your code can be repeated and write functions and loops to accomplish this!
"""

and I did it, apart from the 1 thing.. my code consists of 34 code lines and they require it ready in 16... Cant seem to find a way round it.
Could you please help me out with it?

#defining drawing of horizontal row
def create_horizontal_row():
    for i in range(4):
        pendown()
        left(90)
        forward(50)
#defining drawing of vertical row
def create_vertical_row():
    for k in range(4):
        pendown()
        left(90)
        forward(50)
#creating vertical spacing
def create_space_vertical():
    left(90)
    forward(50)
    right(90)
    
penup()
setposition(-150,-200)
for i in range(8): #creating 8 horizontal top squares
    create_horizontal_row()
    forward(50)
penup()
setposition(-150, 150)
for i in range(8): #creating 8 horizontal top squares
    create_horizontal_row()
    forward(50)
penup()
setposition(-150, -150)
for k in range(6): #creating left vertical row
    create_vertical_row()
    create_space_vertical()
penup()
setposition(200, -150)
for k in range(6): #creating right vertical row
    create_vertical_row()
    create_space_vertical()
added some commentary to what I did
including a screenshot of the final effect
[Image: 8ctdCYK]

Thank you very much in advance for your time and help :)


RE: Tracy the turtle code shortening - buran - Sep-06-2020

don't you see how many repeating code you have? e.g. create_horizontal_row() and create_vertical_row(), lines 19-23 and 24-28, 29-33 and 34-38 are virtually the same