Recursion
Singing “99 Bottles Of Beer On The Wall” is a long and tedious task. Maybe it’d be better to automate it!
#!/usr/bin/env python
bottlephrases = [ (str(bottle) if bottle else “no more” ) + ” bottle” + (“” if bottle == 1 else “s”) + ” of beer on the wall” for bottle in range(100) ]
def verse (bottles):
print (bottlephrases[bottles] + “, ” + bottlephrases[bottles] + “.”).capitalize()
if bottles:
nextaction = “Take one down and pass it around,”
else:
nextaction = “Go to the store and buy some more,”
print nextaction, bottlephrases[(bottles-1) % 100] + “.\n”
if bottles:
verse (bottles-1)
verse (99)
Computers can make a *lot* of tedious, repetitive tasks happen quickly. But there are some repetitive tasks that we should reserve to ourselves because they’re *fun* to do. Let the machine sing the song while you do the drinking! (And keep in mind that this kind of thing is quite magical to people who can’t do it!)