Functions are defined using the def statement. Functions can return multiple values in the form of a tuple. Another cool feature in python is the ability to set default values for parameters.
[code lang=”python”]
#critical paranoia
import random
def selRand(a):
“select a random element from a list”
index = random.randrange(0, len(a))
return a[index]
def crit(pos, cheeses, animals):
“prints a sentence”
sentence = “My ” + selRand(pos)
sentence += ” is like ” + selRand(cheeses)
sentence += “; it is ” + selRand(animals)
print sentence + “.”
return
a = [”ipod”, “laptop”, “shirt”, “cigarette lighter”, “keychain”, “coffee mug”]
b = [”camenbert”, “limburger”, “emmenthaler”, “manchego”, “reggiano”, \
“monterrey jack”]
c = [”known to travel in packs”, “able to run 70mph”, \
“found in the serengheti”, “striped”, “nocturnal”, \
“very aggressive when provoked” ]
crit(a, b, c)
[/code]