Language Python
(Fully compliant version)
Date: | 01/15/06 |
Author: | Ricardo Garcia Gonzalez |
URL: | n/a |
Comments: | 7 |
Info: | http://www.python.org/ |
Score: | ![]() |
#!/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 | ![]() ![]() |
Advanced, extensible beer/wall framework | Jamie Turner | 05/17/06 | 7 | ![]() ![]() |
Creative version | Schizo | 11/06/05 | 16 | ![]() ![]() |
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
<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>
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).