CPSC120A
Fundamentals of Computer Science I

Activity 5

Exploring code tracing with turtle graphics.

Tracing

Being able to read code and figure out what it will do when executed is called tracing the code. Tracing code is an important skill to have for debugging runtime errors. Tracing code helps figure out where the actual execution of a program deviates from the expected execution and thereby determine where and how to fix it.

  1. Draw what the following Python program will draw when run.

    import turtle
    
    line_length = 100
    turn_angle = 90
    turtle.forward(line_length)
    turtle.left(turn_angle)
    
    line_length = 75
    turtle.forward(line_length)
    turtle.left(turn_angle)
    
    line_length = 50
    turtle.forward(line_length)
    turtle.left(turn_angle)
    
    line_length = 25
    turtle.forward(line_length)
    turtle.left(turn_angle)
    
    turtle.mainloop()
  2. Draw what the following Python program will draw when run.

    import turtle
    
    line_length = 100
    turn_angle = 90
    turtle.left(turn_angle)
    turtle.forward(line_length)
    
    line_length = 75
    turtle.left(turn_angle)
    turtle.forward(line_length)
    
    line_length = 50
    turtle.left(turn_angle)
    turtle.forward(line_length)
    
    line_length = 25
    turtle.left(turn_angle)
    turtle.forward(line_length)
    
    turtle.mainloop()
  3. Draw what the following Python program will draw when run.

    import turtle
    
    x = 100
    y = 100
    turtle.goto(x, y)
    turtle.up()
    
    x = -100
    y = 100
    turtle.goto(x, y)
    turtle.down()
    
    x = 100
    y = -100
    turtle.goto(x, y)
    turtle.up()
    
    x = -100
    y = -100
    turtle.goto(x, y)
    turtle.down()
    
    turtle.mainloop()
  4. Draw what the following Python program will draw when run.

    import turtle
    
    x = 100
    y = 100
    turtle.up()
    turtle.goto(x, y)
    
    x = -100
    y = 100
    turtle.down()
    turtle.goto(x, y)
    
    x = 100
    y = -100
    turtle.up()
    turtle.goto(x, y)
    
    x = -100
    y = -100
    turtle.down()
    turtle.goto(x, y)
    
    turtle.mainloop()