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)

Dani AI

Generated

The underlying lesson from this thread is to keep color state in your program rather than relying on the turtle object as the source of truth. saw the symptoms (the first H not staying blue or later Hs drawing in the wrong color) and offered a working fix. To avoid similar surprises and make the recursion simpler to reason about, pass the intended color (or a small boolean) down the recursion so every call knows exactly which color to use.

A minimal pattern (no drawing details) looks like this:

def draw_h(depth, size, color):
    turtle.pencolor(color)        # set the pen explicitly for this node
    if depth == 0:
        return
    # draw the three strokes for this H here...
    next_color = 'orange' if color == 'blue' else 'blue'
    draw_h(depth-1, size/2, next_color)
    draw_h(depth-1, size/2, next_color)
    draw_h(depth-1, size/2, next_color)

Other practical tips not yet covered in detail here: use turtle.pencolor() if you ever need to read the pen color, or keep color values entirely in your variables so comparisons are predictable (some turtle implementations normalize color formats). For performance and visual cleanliness, hide the turtle, set the fastest speed, and use turtle.tracer(0) with turtle.update() at the end of big draws. If toggling on recursion depth is simpler, compute color from depth % 2 so the root is guaranteed blue. For Python 3 compatibility, remember print() and turtle.done() differ from old examples.

For reference on the API and more ways to manage colors and drawing state, see the official Python turtle docs: turtle — Turtle graphics — Python 3 documentation.

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.