Alright, so for the lab I have to do using turtle in python I have to draw an H and then draw H's off of each end of the first H at a 45 degree angle and switch the color between blue and orange. The first H has to be blue and then alternate from there.

I have the drawing of the H's done but I can't figure out how to make it draw the right color without it either drawing back over itself with the wrong color or not having the first H be blue.

I feel like my check for the turtle color may be wrong. I got rid of the use of the check function from the program because it wasn't working.
This is the code that I have so far:

def Initilize():
    turtle.clearscreen()
    turtle.left(90)
    turtle.color('blue')

def Check():
    if turtle.color == 'orange':
        turtle.color('blue')
    else:
        turtle.color('orange')
    
def DrawH(n,s):
    if n == 0:
        return
    else:
        turtle.left(90)
        turtle.forward(s/2)
        turtle.right(90)
        turtle.forward(s/2)
        turtle.left(45)
        DrawH(n-1,s/2)
        turtle.right(45)
        turtle.back(s)
        turtle.right(180)
        turtle.right(45)
        DrawH(n-1,s/2)
        turtle.left(45)
        turtle.right(180)
        turtle.forward(s/2)
        turtle.right(90)
        turtle.forward(s)
        turtle.left(90)
        turtle.forward(s/2)
        turtle.right(45)
        DrawH(n-1,s/2)
        turtle.left(45)
        turtle.right(180)
        turtle.forward(s)
        turtle.left(45)
        DrawH(n-1,s/2)
        turtle.right(45)
        turtle.right(180)
        turtle.forward(s/2)
        turtle.left(90)
        turtle.forward(s/2)
        turtle.right(90)

Recommended Answers

All 5 Replies

You should use routine to draw single h with parameter for size and function repeating to draw h by loop doing total 360 degrees turn with parameter for amount of turn. Your colour alternation routine is fine, even there is many ways to do it.

The function to draw the picture works fine so I don't really want to mess with it even if it isn't the most efficient method. But when I try to check for change before running the function again in the function it gets stuck on orange, which makes me think that the if statement is wrong and its just defaulting to setting to orange every time after the start.

turtle.color seems to have two values, additionaly you are comparing the function turtle.color to string, which is allways False.

import turtle

def Initialize():
    turtle.clearscreen()
    turtle.left(90)
    turtle.color('orange')

def Check():
    print 'check, turtle.color %s' % turtle.color()[0]
    if turtle.color()[0] == 'orange':
        turtle.color('blue')
    else:
        turtle.color('orange')
    
def DrawH(n,s):
    if n == 0:
        return
    else:
        Check()
        turtle.left(90)
        turtle.forward(s/2)
        turtle.right(90)
        turtle.forward(s/2)
        turtle.left(45)
        DrawH(n-1,s/2)
        turtle.right(45)
        turtle.back(s)
        turtle.right(180)
        turtle.right(45)
        DrawH(n-1,s/2)
        turtle.left(45)
        turtle.right(180)
        turtle.forward(s/2)
        turtle.right(90)
        turtle.forward(s)
        turtle.left(90)
        turtle.forward(s/2)
        turtle.right(45)
        DrawH(n-1,s/2)
        turtle.left(45)
        turtle.right(180)
        turtle.forward(s)
        turtle.left(45)
        DrawH(n-1,s/2)
        turtle.right(45)
        turtle.right(180)
        turtle.forward(s/2)
        turtle.left(90)
        turtle.forward(s/2)
        turtle.right(90)
        Check()

Initialize()
DrawH(2, 300)
turtle.mainloop()

Without doubled lines:

import turtle

def Initialize():
    turtle.clearscreen()
    turtle.delay(0)
    turtle.ht()
    turtle.left(90)
    turtle.color('orange')

def Check():
    if turtle.color()[0] == 'orange':
        turtle.color('blue')
    else:
        turtle.color('orange')
    
def DrawH(n,s):
    if n:
        Check()
        # go to first corner to do H there pen up
        turtle.pu()
        turtle.left(90)
        turtle.forward(s/2)
        turtle.right(90)
        turtle.forward(s/2)
        turtle.left(45)
        turtle.pd()      

        DrawH(n-1,s/2)
        turtle.right(45)
        turtle.back(s)
        turtle.right(180+45)
        DrawH(n-1,s/2)

        turtle.pu()
        turtle.right(180-45)
        turtle.forward(s/2)
        turtle.right(90)
        turtle.pd()
        
        turtle.forward(s)

        turtle.pu()       
        turtle.left(90)
        turtle.forward(s/2)
        turtle.right(45)
        turtle.pd()

        DrawH(n-1,s/2)
        turtle.right(180-45)
        turtle.forward(s)
        turtle.left(45)
        DrawH(n-1,s/2)

        # return home from last corner pen up
        turtle.pu()
        turtle.right(180+45)
        turtle.forward(s/2)
        turtle.left(90)
        turtle.forward(s/2)
        turtle.right(90)
        turtle.pd()

        Check()

Initialize()
DrawH(4, 300)
turtle.mainloop()

Yeah that fixed it, thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.