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:15
Info:http://www.python.org/
Score: (3.75 in 822 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/0516
Advanced, extensible beer/wall frameworkJamie Turner05/17/067
minimal versionOliver Xymoron04/20/055
using lambda in LISP styleJ Adrian Zimmer11/14/062
Exception basedMichael Galpin02/08/080
functional, w/o variables or proceduresIvan Tkatchev07/14/052
minimal version with singularEmlyn Jones06/13/053
Fully compliant versionRicardo Garcia Gonzalez01/15/067
Using a iterator classEric Moritz01/20/062
New conditional expressions in 2.5Ezequiel Pochiero12/18/061

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

>>  HR said on 10/16/08 10:29:51

HR msg = '''%s bottles of beer on the wall,
%s bottles of beer,
Take one down, pass it around,
%s bottles of beer on the wall'''
print '\n\n'.join([msg % ((i,)*3) for i in reversed(range(1,100))])
print '''No bottles of beer on the wall,
No bottles of beer,
Go to the store and buy some more,
No bottles of beer on the wall'''

>>  Wimma said on 11/07/08 23:46:00

Wimma recursive:
#!/usr/bin/python
# -*- coding: utf8 -*-

def bottles(a):
print str(a)+" bottles of beer on the wall,"
print str(a)+" bottles of beer."
print "Take one down pass it around"
print str(a-1)+" bottles of beer on the wall!"
print
if a>1:
bottles(a-1)

bottles(99)

>>  gogy said on 11/15/08 02:22:59

gogy def bottles(a):
print a,"bottles of beer on the wall,", a ,"bottles of beer."
print "Take one down pass it around", a-1,"bottles of beer on the wall\n"
if a>2:
bottles(a-1)

bottles(99)

print """
1 bottle of beer on the wall, 1 bottle of beer.
Take one down 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."""

>>  baba said on 11/16/08 23:08:18

baba #!/usr/bin/python
# aspect oriented

import aspects

numberOfBottles = 99

def takeBottle():
global numberOfBottles
if numberOfBottles > 0:
numberOfBottles = numberOfBottles - 1
else:
numberOfBottles = 99

def writeText(*args, **kwargs):
print "\n", numberOfBottles, "bottles of beer on the wall,", numberOfBottles, "bottles of beer."
yield aspects.proceed(*args, **kwargs)
if numberOfBottles > 1:
print "Take one down and pass it around,", numberOfBottles, "bottles of beer on the wall."
else:
print "Take one down and pass it around,", numberOfBottles, "bottle of beer on the wall."

def writeTextLastBottle(*args, **kwargs):
print "\n", numberOfBottles, "bottle of beer on the wall,", numberOfBottles, "bottle of beer."
yield aspects.proceed(*args, **kwargs)
print "Take one down and pass it around, no more bottles of beer on the wall."

def writeTextBuySomeMore(*args, **kwargs):
print "\nNo more bottles of beer on the wall, no more bottles of beer."
yield aspects.proceed(*args, **kwargs)
print "Go to the store and buy some more,", numberOfBottles, "bottles of beer on the wall."

aspects.with_wrap(writeText, takeBottle)
while numberOfBottles > 1:
takeBottle()
aspects.without_wrap(writeText, takeBottle)
aspects.with_wrap(writeTextLastBottle, takeBottle)
takeBottle()
aspects.without_wrap(writeTextLastBottle, takeBottle)
aspects.with_wrap(writeTextBuySomeMore, takeBottle)
takeBottle()

>>   said on 02/04/09 21:38:59

Another one-liner, but only 318 bytes:

print'\n\n'.join(map(lambda v:'%s bottle%s of beer on the wall, %s bottle%s of beer.\n%s, %s bottle%s of beer on the wall.'%([v,'No'][v==0],['s',''][v==1],[v,'no'][v==0],['s',''][v==1],['Take one down, pass it around','Go to the store and buy some more'][v==0],[v-1,99][v==0] or'No',['s',''][v-1==1]),range(99,-1,-1)))

>>  Shea Kauffman said on 01/25/10 23:39:13

Shea Kauffman Functional Programming Style


#!/usr/bin/env python
# -*- coding: utf-8 -*-

def bottles(n):
bottlesay = {}
bottlesay[0] = "no more bottles"
bottlesay[1] = "1 bottle"
return bottlesay[n] if n in bottlesay else str(n)+" bottles"

def verse(n):
if n < 1:
return "No more bottles of beer on the wall, no more bottles of beer.\n Go to the store and buy some more, 99 bottles of beer on the wall."
else:
return bottles(n)+" of beer on the wall, "+bottles(n)+" of beer.\n Take one down and pass it around, "+bottles(n-1)+" of beer on the wall.\n"

if __name__ == '__main__':
def sing(x):
print verse(x)
map(sing, reversed(range(100)))

>>  max said on 02/09/10 01:08:56

max #Very Simple
def BG():
x = 99
bottle = " bottles"
while x > 0:
print x, bottle, " of beer on the wall, ", x, bottle, " of beer."
x = x - 1
if x <= 1:
bottle = " bottle"
print "Take one down pass it arround, ", x, bottle, " of beer on the wall!"
print

>>  bob son said on 03/12/10 13:17:32

bob son good point of you
http://www.switchonthecode.com/

>>  billybackwater said on 08/13/10 00:11:20

billybackwater Python is the Coldplay of programming languages.

>>  symetrik said on 09/19/10 07:34:25

symetrik Python, a language meant to do simple things, simply.

for bottle in range(99): print "%s bottles of coke on the wall, %s bottles of coke. Take one down, pass it around, %s bottles of coke on the wall" % (99-bottle, 99-bottle, 99-bottle-1)

>>  Nick said on 09/22/10 21:10:46

Nick import sys

def beerSong():
word = "bottles"
for quant in range(99,-1,-1):
if quant > 1:
print str(quant)+ " " + word + " of beer on the wall, " +str(quant)+ " " + word + " of beer."
print "Take one down, pass it around.\n"
elif quant >= 0:
if quant == 1:
word = "bottle"
print str(quant)+ " " + word + " of beer on the wall, " +str(quant)+ " " + word + " of beer."
print "Take one down, pass it around, no more bottles of beer on the wall.\n"
elif quant == 0:
print "No more bottles of beer on the wall, no more bottles of beer."
print "Go to the store and buy some more, 99 bottles of beer ont he wall.\n"
else:
sys.exit(0)
else:
sys.exit(0)

beerSong()

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: