[Tutor] Function help

Scott W Dunning swdunning at cox.net
Mon Feb 24 03:31:42 CET 2014


On Feb 23, 2014, at 2:26 AM, Peter Otten <__peter__ at web.de> wrote:
> which still shows a repetetive pattern and thus you can simplify it with 
> another loop. You should be able to find a way to write that loop with two
> star_row() calls on a single iteration, but can you do it with a single call 
> too?

So, I was able to cut it down a bit but I’m having a hard time trying to cut it down with another loop and a single call.  Wouldn’t what you’re saying in the above paragraph have to be another function with a loop inside?  Or are you saying just another loop will suffice?  Any hints?  Here is what I got so far, I’ll put the loop in question in bold. 

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)

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

def star_row(numberstars):
    for i in range (numberstars):
        fillstar(red)
        space(25)
        
star_row(5)
row(25)
star_row(5)
row(25)
star_row(6)
row(25)
star_row(5)
row(25)
star_row(6)
row(25)
star_row(5)
row(25)
star_row(6)
row(25)
star_row(5)
row(25)
star_row(6)


This is what I’m thinking…

for I in range(4)
	star_row(5)
	row(25)
	star_row(6)
	row(25)


????  Am I at all close?


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


More information about the Tutor mailing list