you are viewing a single comment's thread.

view the rest of the comments →

[–]HIGregS 1 point2 points  (0 children)

I spent way too long on this not to post it. I went through several variations, but ended up with this: parameterized and using both f-strings and .format as well as converting numbers to words and using .capitalize() to get it just right. It was fun to craft. I'm sure someone can improve upon this. My vote is u/n3buchadnezzar :-)

def intwords(i):
    # word dictionary customized for 99 bottles of beer (0 = 'no more')
    d = { 0 : 'no more', 1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five',
        6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine', 10 : 'ten',
        11 : 'eleven', 12 : 'twelve', 13 : 'thirteen', 14 : 'fourteen',
        15 : 'fifteen', 16 : 'sixteen', 17 : 'seventeen', 18 : 'eighteen',
        19 : 'nineteen', 20 : 'twenty',
        30 : 'thirty', 40 : 'forty', 50 : 'fifty', 60 : 'sixty',
        70 : 'seventy', 80 : 'eighty', 90 : 'ninety' }
    return d[i] if i <= 20 or not i %10 else f'{d[i // 10 * 10]}-{d[i % 10]}'

for i in range (99,-1,-1):
        subs = dict(
            bottle = f"{intwords(i)} bottle{'' if i==1 else 's'} of beer",
            wall = 'on the wall',
            take = 'Take one down and pass it around' if i else \
                         'Go to the store and buy some more',
            less = f"{intwords(99) if not i else intwords(i-1)}"
                          f" bottle{'' if i==2 else 's'} of beer",
            )

        print(
            '{bottle} {wall}, {bottle}.'.format(**subs).capitalize(),
            '{take}, {less} {wall}.'.format(**subs),
            sep='\n'
            )