beer.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def song(a, b=0):
    return '\n'.join(map(verse, range(a, b-1, -1))) + '\n'


def bottles(n):
    """Say that there are n bottles of beer"""
    return '{num} bottle{plural} of beer'.format(
        num=(str(n) if n else 'no more'),
        plural=('' if n == 1 else 's')
    )


def verse(n):
    line1 = "{0} on the wall, {0}.".format(bottles(n)).capitalize()
    if n > 0:
        article = 'it' if n == 1 else 'one'
        what_to_do = "Take {} down and pass it around".format(article)
        remaining = "{} on the wall.".format(bottles(n-1))
    else:
        what_to_do = "Go to the store and buy some more"
        remaining = "{} on the wall.".format(bottles(99))

    line2 = ', '.join((what_to_do, remaining))

    return '\n'.join((line1, line2, ''))

Comments


You're not logged in right now. Please login via GitHub to comment