[Tutor] Function help

Peter Otten __peter__ at web.de
Mon Feb 24 10:41:33 CET 2014


Scott W Dunning wrote:

> 
> On Feb 23, 2014, at 2:26 AM, Peter Otten <__peter__ at web.de> wrote:
>> If you want to make rows with more or less stars, or stars in other
>> colors you could add parameters:
>> 
>> def star_row(numstars, starcolor):
>>    for i in range(numstars):
>>        fillstar(starcolor)
>>        space(25)
>> 
>> Your code will then become
>> 
>> star_row(6, red)
>> row(25)
>> star_row(5, red)
>> row(25)
>> 
> I have a question with the above loop function.  Why couldn’t row(25) be
> added into the function so that wouldn’t have to placed in between every
> star_row()?

That's of course possible.

>> 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?
> Not sure I understand what you mean in the above paragraph?

What you found out later and put in you next post:

This repetetive pattern

> star_row(5) # oops, typo
> 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)

can be turned into this for loop:

> 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?

You hit the jackpot :) Now on to the next challenge:

for row_index in range(9):
     row_width = ...
     star_row(row_width)
     row(25)

Can you replace the ... with a calculation to get row_width=6 for even and 
row_width=5 for odd rows? 

Hint: look at the % ("modulo") operator.



More information about the Tutor mailing list