Language Python
(Using a iterator class)
| Date: | 01/20/06 |
| Author: | Eric Moritz |
| URL: | http://eric.themoritzfamily.com |
| Comments: | 2 |
| Info: | http://www.python.org |
| Score: |
class Song:
def __init__(self):
self.n = 100
def __iter__(self):
return self
def next(self):
if self.n == 0:
raise StopIteration
self.n = self.n - 1
return self._get_verse(self.n)
def _get_verse(self,n):
lines = []
data = []
if n > 1:
data.append(["%s bottles" % n,"%s bottles" % n])
data.append(["Take one down pass it around","%s bottles" % (n-1)])
elif n == 1:
data.append(["%s bottle" % n,"%s bottle" % n])
data.append(["Take one down pass it around","%s bottles" % "no more"])
elif n == 0:
data.append(["No more bottles","no more bottles"])
data.append(["Go to the store and buy some more","%s bottles" % 99])
lines.append("%s of beer on the wall, %s of beer" % tuple(data[0]))
lines.append("%s, %s of beer on the wall" % tuple(data[1]))
lines.append("")
return "\n".join(lines)
song = Song()
for stanza in song:
print stanza
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 | |
| using lambda in LISP style | J Adrian Zimmer | 11/14/06 | 2 | |
| minimal version | Oliver Xymoron | 04/20/05 | 5 | |
| functional, w/o variables or procedures | Ivan Tkatchev | 07/14/05 | 2 | |
| Fully compliant version | Ricardo Garcia Gonzalez | 01/15/06 | 7 | |
| minimal version with singular | Emlyn Jones | 06/13/05 | 3 | |
| Exception based | Michael Galpin | 02/08/08 | 0 | |
| 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