Python Forum
Tracy the turtle code shortening
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tracy the turtle code shortening
#1
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 :)
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating code to make up to 4 turtle move simultaneously in a random heading J0k3r 3 5,462 Mar-05-2018, 03:48 PM
Last Post: mpd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020