0

I have a project to do for my CMPT 120 Class. And it's using basic python coding.
I'm trying to write my name out with block letters. 'JAX'
I need to use for loops
and i'm stuck on my X
here's the code for it

def x(n):
    t3=turtle.Pen()
    
    t3.left(60)

    t3.forward(n)
    t3.left(60)
    t3.forward(n)
    t3.right(120)
    t3.forward(n)
    t3.right(60)
    t3.forward(n/2)
    t3.left(120)
    t3.forward(n/2)
    t3.right(60)
    t3.forward(n)

    t3.right(120)

    t3.forward(n)
    t3.left(60)
    t3.forward(n)
    t3.right(120)
    t3.forward(n)
    t3.right(60)
    t3.forward(n/2)
    t3.left(120)
    t3.forward(n/2)
    t3.right(60)
    t3.forward(n)

I've separated it in parts that are similar which i can loop, but there is the
t3.right(120)
that's odd in the loop, is there a way i can still use a for loop?
thanks
ha, i need help asap, project is due tomorrow.
Thanks
Jackie

3
Contributors
3
Replies
5
Views
11 Years
Discussion Span
Last Post by vegaseat
0

I'm confused as to what you are trying to do.

Can you explain it again using pseudo code?

e.g.

perform this loop 10 times
action1
action2
end loop

0

I'm confused as to what you are trying to do.

Can you explain it again using pseudo code?

e.g.

perform this loop 10 times
   action1
   action2
end loop
0

This would be one way to shorten your code with a for loop:

import turtle
import time

def x(n):
    "draw a turtle x of size n, shortened version using a for loop"
    t3=turtle.Pen()

    for k in (1, 2):
        if k == 1:
            t3.left(60)
        else:
            t3.right(120)

        t3.forward(n)
        t3.left(60)
        t3.forward(n)
        t3.right(120)
        t3.forward(n)
        t3.right(60)
        t3.forward(n/2)
        t3.left(120)
        t3.forward(n/2)
        t3.right(60)
        t3.forward(n)

x(30)
time.sleep(3)  # show test for some seconds (just for test)

Nice project!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.