99 Bottles of Beer (Python)

From LiteratePrograms

Jump to: navigation, search
Other implementations: Alice ML | Erlang | Haskell | Inform 7 | Java | OCaml | Perl | Python | Ruby

A typical verse of the 99 bottles of beer song is:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.

The verse is then repeated starting with the new count for the number of bottles left on the wall:

98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.

The last three verses (two, one and zero bottles of beer on the wall) are special cases:

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall.

No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.

Note that when we get to one bottle of beer the verse changes from the plural bottles to the singular bottle, and the final line No more bottles of beer on the wall! of the song is different from all the preceding verses. The very last verse is also different from all the others.

The implementation

Because the song is so structured, with only the number of bottles changing in most verses, and pluralization and the next action to take changing in a few other verses, the song can easily be structured as a loop. Perharps the easiest way of the program is the while loop.

bottle_number = 99                            #The starting point
countdown = 99                                #And the counter
while bottle_number > 2:                      #Beginning of the loop
    print bottle_number, "bottles of beer on the wall,", bottle_number, "bottles of beer."
    print "Take one down and pass it around,", bottle_number - 1, "bottles of beer on the wall."
    print " "
    countdown = countdown - 1
    bottle_number = countdown
if bottle_number == 2:                        #From here on it's easier to do the rest "manually"
    print """2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall.
No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
"""

This is quite a primitive way but it works!

A far more elegant approach:

verse = """
%s of beer on the wall. %s of beer.
Take one down and pass it around. %s of beer on the wall.
"""
def pluralize(n):
   if n > 1:
      return "%d bottles" % n
   elif n < 1:
      return "No more bottles"
   else:
      return "1 bottle"
for eachBottle in range(99, 0, -1):
   current = pluralize(eachBottle)
   print verse % (current, current, pluralize(eachBottle - 1))
lastverse = """
No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
"""
print lastverse

This example simply uses a template string and Python's string interpolation operator (the "%" operator as overloaded for strings) along with the built-in range() function. Proper pluralization of the word "bottles" and the "No more" case is handled in it's own function.

Views