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

(Fully compliant version)

Date:01/15/06
Author:Ricardo Garcia Gonzalez
URL:n/a
Comments:7
Info:http://www.python.org/
Score: (2.73 in 52 votes)
#!/usr/bin/env python
# 	99 bottles of beer in Python by Ricardo Garcia. Public Domain code.
# 	Fully compliant version, pretty indentation, fits in 80x24.

plural = lambda n: n != 1 and "s" or ""
number = lambda n: n == 0 and "No" or str(n)
next = lambda n: (n - 1) % 100

pu_line = lambda n: n == 0 and	"Go to the store and buy some more" or	\
				"Take one down, pass it around"

verses = lambda n:	"%s bottle%s of beer on the wall!\n"		\
			"%s bottle%s of beer!\n"			\
			"%s\n"						\
			"%s bottle%s of beer on the wall!\n" %		\
			(	number(n), plural(n),
			 	number(n), plural(n),
			 	pu_line(n),
			 	number(next(n)), plural(next(n))	)

print "\n".join([verses(x) for x in range(99, -1, -1)])

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
This example demonstrates the simplicityGerold Penz07/23/0515
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
Using a iterator classEric Moritz01/20/062
New conditional expressions in 2.5Ezequiel Pochiero12/18/061

Comments

>>  gp said on 12/08/06 23:17:13

gp very pythonic except "and/or" paradigm. maybe it's time to rewrite it for 2.5 using "a if cond else b"?

>>  Ricardo Garcia Gonzalez said on 12/31/06 15:17:13

Ricardo Garcia Gonzalez Indeed. I should have probably omitted the brackets in the last line, and used xrange instead of range. :)

>>  LR said on 03/19/07 15:51:45

LR Calling the code above "pythonic" is not really helpful. Python is an very readable language, but this example is anything but.

>>  Ricardo Garcia Gonzalez said on 03/29/07 19:15:13

Ricardo Garcia Gonzalez I have updated the script for Python 2.5. I'm not sure I should post it as a new version, but in the mean time, I'll use the comments section.

<pre>
#!/usr/bin/env python
# 99 bottles of beer in Python by Ricardo Garcia. Public Domain code.
# Fully compliant version, pretty indentation, fits in 80x24.
# This version requires Python 2.5.

def plural(n): return ('s' if n != 1 else '')
def numtxt(n): return (str(n) if n != 0 else 'No')
def next(n): return ((n - 1) % 100)

def line_1_4(n): return ('%s bottle%s of beer on the wall!'
% (numtxt(n), plural(n)))

def line_2(n): return ('%s bottle%s of beer!'
% (numtxt(n), plural(n)))

def line_3(n): return ('Take one down, pass it around' if n != 0
else 'Go to the store and buy some more')

def verses(n): return ('%s\n%s\n%s\n%s\n'
% (line_1_4(n), line_2(n),
line_3(n), line_1_4(next(n))))

print '\n'.join(verses(x) for x in xrange(99, -1, -1))
</pre>

>>  Ricardo Garcia Gonzalez said on 03/29/07 19:16:22

Ricardo Garcia Gonzalez Aw, fuck. I suspected it was going to screw it up... and it did. Sorry. :(

>>  Joe Pythonprogrammer said on 12/18/08 23:59:32

Joe Pythonprogrammer You shouldn't have called it the fully compliant version. You didn't follow the PEP that sets indentation to be four spaces; Instead, you used tabs!

>>  Ricardo Garcia said on 08/30/09 02:15:35

Ricardo Garcia I called it "fully compliant" version because at the time I wrote the program there were many versions in this website that didn't print the lyrics correctly to save some lines and make it simpler. It has nothing to do with source code indentation.

By the way, the PEP you refer to is PEP-8, which does NOT forbid using tabs to indent code. Only mixing tabs and spaces is explicitly prohibited. Using 4 spaces is "strongly recommended" (sic).

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: