Q:

Fixing function after debugging

I am trying to debug and fix this function where it is suppose to draw capital "i" when the depth is 2. I think I found the problem using pdb.set_trace() but not too sure how to fix it overall. This function results in half of capital "i". And in anyway I try to fix the function by changing it, it will mess it up even more... Here is the function.
from turtle import *
import pdb

def initWindow( size ):
    setup(600,600)
    setworldcoordinates( -size-1, -size-1, size, size )
    left(90)
    pensize(3)
    speed(0)

def drawI(depth, size):
    if(depth < 1):
        pass
    else:
        pdb.set_trace()
        fd(size/2)
        lt(90)
        drawI(depth-1,size/2)
        rt(90)  ###############HERE
        fd(-size/2)
        lt(90)
        drawI(depth-1,size/2) #Where it seems to be the problem
        rt(90)

def main():
    size = 100
    depth = int(input("enter depth: "))
    initWindow(size)
    drawI(depth, size)
    input("enter to quit")

main()

One of my question is Why is the second drawI(depth-1,size/2) ignored and goes to rt(90) and then goes back to #HERE after?

A:

You need to specify your forward commands so the turtle starts at the center of the line, and draws one half of the line in front of it and one half behind it. Like this:
def drawI(depth, size):
    if(depth < 1):
        pass
    else:
        #draw the half in front of the turtle
        fd(size/2)

        #draw head
        lt(90)
        drawI(depth-1,size/2)
        rt(90)

        #return to center
        fd(-size/2)

        #draw the half behind the turtle
        fd(-size/2)

        #draw head
        lt(90)
        drawI(depth-1,size/2)
        rt(90)

        #return to center
        fd(size/2)

Your original code started the turtle at the end of the line, which caused unusual results when you recursed into lower depths.

