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
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
This example demonstrates the simplicity | Gerold Penz | 07/23/05 | 15 | |
Creative version | Schizo | 11/06/05 | 16 | |
Advanced, extensible beer/wall framework | Jamie Turner | 05/17/06 | 7 | |
minimal version | Oliver Xymoron | 04/20/05 | 5 | |
using lambda in LISP style | J Adrian Zimmer | 11/14/06 | 2 | |
Exception based | Michael Galpin | 02/08/08 | 0 | |
functional, w/o variables or procedures | Ivan Tkatchev | 07/14/05 | 2 | |
minimal version with singular | Emlyn Jones | 06/13/05 | 3 | |
Using a iterator class | Eric Moritz | 01/20/06 | 2 | |
New conditional expressions in 2.5 | Ezequiel Pochiero | 12/18/06 | 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!
Comments
gp said on 12/08/06 23:17:13
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
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
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
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
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
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
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).