[Tutor] Function help

Scott W Dunning swdunning at cox.net
Mon Feb 24 03:04:56 CET 2014


On Feb 23, 2014, at 2:26 AM, Peter Otten <__peter__ at web.de> wrote
> a programmer would think "for loop” immediately
That’s what I thought.  It just seemed like way to much to keep repeating everything over and over.  I knew there had to be a better way we just haven’t learned loops in school yet.  
> 
> for i in range(5):
>    fillstar(red)
>    space(25)
Awesome, I was able to cut everything down quite a bit but, now like you say below I’m still repeating the loop.  I’m gonna see if I can make a function with the loop to cut it down even further.  

Here is what I was able to cut it down to so far with your help.  I’ll paste the new code when I make a function with the loop and maybe you guys can help me see if it look any better/easier.  

Also, does anyone know anything about turtle where I can try and move the starting point to the upper left hand corner?  

Thanks again!
Scott


from turtle import *
from math import sin, sqrt, radians

def star(width):
    R = (width)/(2*sin(radians(72)))
    A = (2*width)/(3+sqrt(5))
    penup()
    left(18)
    penup()
    forward(R)
    pendown()    
    left(162)
    forward(A)
    right(72)
    forward(A)    
    left(144)
    forward(A)
    right(72)
    forward(A)
    left(144)
    forward(A)
    right(72)
    forward(A)    
    left(144)
    forward(A)
    right(72)
    forward(A)
    left(144)
    forward(A)
    right(72)
    forward(A)
    penup()
    left(162)
    forward(R)
    left(162)
    
showturtle()

def fillstar(color):
    fillcolor(color)
    begin_fill()
    star(25)
    end_fill()
    
red = "red"
fillstar(red)

def space(width):
    penup()
    forward(2*width)
    pendown()

space(25)

for i in range (5):
    fillstar(red)
    space(25)

def row(width):
    penup()
    right(90)
    forward(width)
    right(90)
    forward(11*width)
    right(180)
    pendown()
row(25)

for i in range (5):
    fillstar(red)
    space(25)

row(25)

for i in range (6):
    fillstar(red)
    space(25)

row(25)

for i in range (5):
    fillstar(red)
    space(25)

row(25)

for i in range (6):
    fillstar(red)
    space(25)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140223/8a12a9eb/attachment-0001.html>


More information about the Tutor mailing list