I suggest this
from turtle import *
def snowflake(lengthSide, levels):
if levels == 0:
forward(lengthSide)
return
lengthSide /= 3.0
snowflake(lengthSide, levels-1)
left(60)
snowflake(lengthSide, levels-1)
right(120)
snowflake(lengthSide, levels-1)
left(60)
snowflake(lengthSide, levels-1)
if __name__ == "__main__":
speed(0)
length = 300.0
penup()
backward(length/2.0)
pendown()
snowflake(length, 4)
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
how do you modify the code so that it can make a full snowflake?
Simply define
def full_snowflake(lengthside, levels):
for i in range(3):
snowflake(lengthside, levels)
right(120)
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
And put at the end of program:
mainloop()
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
from turtle import *
def snowflake(lengthSide, levels):
if levels == 0:
forward(lengthSide)
return
lengthSide /= 3.0
snowflake(lengthSide, levels-1)
left(60)
snowflake(lengthSide, levels-1)
right(120)
snowflake(lengthSide, levels-1)
left(60)
snowflake(lengthSide, levels-1)
def full_snowflake(lengthside, levels):
for i in range(3):
snowflake(lengthside, levels)
right(120)
if __name__ == "__main__":
speed(0)
length = 300.0
penup()
backward(length/2.0)
pendown()
full_snowflake(length, 4)
mainloop()
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
Here is a snowflakes containing printable characters
from turtle import *
import random, string
fact = (2.0/(3.0*sqrt(3.0)), sqrt(3.0)/2.0, 0.5)
def writers(cx, cy, lengthside, levels):
if levels == 0:
penup()
goto(cx, cy)
pendown()
write(random.choice(string.printable))
return
r = lengthside * fact[0]
a = r * fact[1]
b = r * fact[2]
centers = [(cx, cy), (cx, cy+r), (cx, cy-r), (cx+a, cy+b), (cx+a, cy-b), (cx-a, cy+b), (cx-a, cy-b)]
for ux, uy in centers:
writers(ux, uy, lengthside/3.0, levels-1)
if __name__ == "__main__":
speed(0)
length = 500.0
writers(0.0, 0.0, length, 4)
mainloop()
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691