[Tutor] Function help
Dave Angel
davea at davea.name
Sun Feb 23 13:31:29 CET 2014
Scott W Dunning <swdunning at cox.net> Wrote in message:
> I am VERY new to python (programming too). I had a question regarding functions. Is there a way to call a function multiple times without recalling it over and over. Meaning is there a way I can call a function and then add *5 or something like that? I am trying to code an American Flag using turtle for class so I’ll post the code I have so far below. As you can see towards the bottom I recall the functions to draw the stars, fill in color and give it spacing. I was wondering if there was a way to cut down on all that some how?
>
Welcome to the tutor forum also, Scott. You'll find it works very
similarly to python-list, and has many of the same people on it.
I'm not sure how you tried to attach source, but please be aware
that this is a text list - anything other than plain text will
probably be invisible or inconvenient to someone. Just paste
snippets inline when needed.
What you're looking for is a loop. for and while are the two
keywords for looping. In this case, since you know how many
times you want to go round, loop is appropriate. Build a
collection or iterator of length 5, and loop over it. range is
designed for the purpose:
for index in range (5):
dosomething
moresomething (index)
Everything that's indented will happen 5 times. Later you'll want
to learn continue and break, which can alter the simple flow. And
presumably you already know if.
import, for, while, if, elif, else, break, continue
Incidentally, your star function could have been much shorter
with a loop.
--
DaveA
More information about the Tutor
mailing list