Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Python

(This example demonstrates the simplicity)

Date:07/23/05
Author:Gerold Penz
URL:http://gerold.bcom.at/
Comments:4
Info:http://www.python.org/
Score: (3.63 in 65 votes)
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
"""
99 Bottles of Beer (by Gerold Penz)
Python can be simple, too :-)
"""

for quant in range(99, 0, -1):
   if quant > 1:
      print quant, "bottles of beer on the wall,", quant, "bottles of beer."
      if quant > 2:
         suffix = str(quant - 1) + " bottles of beer on the wall."
      else:
         suffix = "1 bottle of beer on the wall."
   elif quant == 1:
      print "1 bottle of beer on the wall, 1 bottle of beer."
      suffix = "no more beer on the wall!"
   print "Take one down, pass it around,", suffix
   print "--"

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Creative versionSchizo11/06/0511
minimal versionOliver Xymoron04/20/053
Advanced, extensible beer/wall frameworkJamie Turner05/17/062
Fully compliant versionRicardo Garcia Gonzalez01/15/065
Using a iterator classEric Moritz01/20/062
using lambda in LISP styleJ Adrian Zimmer11/14/060
functional, w/o variables or proceduresIvan Tkatchev07/14/051
New conditional expressions in 2.5Ezequiel Pochiero12/18/060
Exception basedMichael Galpin02/08/080
minimal version with singularEmlyn Jones06/13/052

Comments

>>  Wen Zhang said on 08/30/06 05:08:39

Wen Zhang Look this one

b=lambda n:'\n%s bottle%s of beer on the wall'%(n%100 or'No more','s'*(n!=1))
for n in range(99,-1,-1):print(b(n)*2)[:-12]+'\n'+'Take one down, pass it \
around'*(n!=0)+'Go to the store and buy some more'*(n==0)+b(n-1)

>>  Wen Zhang said on 08/30/06 14:33:49

Wen Zhang Clean One:

'''99 Bottles of Beer in Python by Wen Zhang. Nature version'''

bottle = ' bottles of beer on the wall'
take = '.\nTake one down and pass it around,'
for n in reversed(range(100)):
nBottle = str(n) + bottle
if n == 1:
nBottle = nBottle.replace('es','e')
if n == 0:
nBottle = 'no more' + bottle
take = '.\nGo to the store and buy some more, 99' + bottle + '.'
if n != 99:
print nBottle + '.\n'
print nBottle.capitalize() + ', ' + nBottle[:-12] + take,

>>  Wen Zhang said on 08/30/06 14:53:03

Wen Zhang For above code, you need to indent certain lines. The post cannot show indents.

>>  Ivan said on 08/31/07 14:52:51

Ivan whole song in one explression :)

print("".join(map(lambda x: x and "%s%d bottle%s of beer on the wall, %d bottle%s of beer...\nTake one down, pass it around.\n"%(x<99 and "%d bottles of beer on the wall.\n\n"%x or "\n", x, x>1 and "s" or " ", x, x>1 and "s" or " ";) or "No bottles of beer on the wall.\n\nNo more bottles of beer...\nGo to the store and buy some more...\n99 bottles of beer.", range(99,-1,-1))))

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: