Functions help

Rhodri James rhodri at wildebst.org.uk
Sun Feb 23 20:01:15 EST 2014


On Sun, 23 Feb 2014 05:43:17 -0000, Scott W Dunning <swdunning at cox.net>  
wrote:

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

The usual way to call a function several times is to use a loop, like this:

   for i in range(5):
       my_function()

The function "range" returns the sequence of numbers 1, 2, 3, 4 and 5 [*],  
so this has the same effect as if you had typed:

   my_function()
   my_function()
   my_function()
   my_function()
   my_function()

This isn't a great advantage if you just want to call the function two or  
three times, but when you want to call it two or three hundred times it  
matters a lot more!  You can still use the same technique if you want to  
pass different parameters to the function each time you call it:

   for i in range(6):
       print(i*i)

   for day in ("Mon", "Tue", "Wed", "Thu", "Fri"):
       do_stuff_for_day(day)

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list