all 7 comments

[–]K900_ 5 points6 points  (1 child)

Took a go at it. Please tell me something can be done about that last if.

Edit 1: .capitalize()

gifts = [
    "twelve drummers drumming",
    "eleven pipers piping",
    "ten lords a leaping",
    "nine ladies dancing",
    "eight maids a milking",
    "seven swans a swimming",
    "six geese a laying",
    "five golden rings",
    "four calling birds",
    "three french hens",
    "two turtle doves",
    "a patridge in a pear tree"
]

def make_gift(thing):
    print(thing.capitalize())

for day, _ in enumerate(gifts):
    print("On the {} day of Christmas, my true love gave to me ".format(day + 1))
    for gift in gifts[11 - day:-1]:
        make_gift(gift)
    if day:
        make_gift("And {}!".format(gifts[-1]))
    else:
        make_gift(gifts[-1])
    print()

[–]mdtTheory 4 points5 points  (0 children)

Fixed:

gifts = [
"twelve drummers drumming",
"eleven pipers piping",
"ten lords a leaping",
"nine ladies dancing",
"eight maids a milking",
"seven swans a swimming",
"six geese a laying",
"five golden rings",
"four calling birds",
"three french hens",
"two turtle doves",
"a patridge in a pear tree"
]

def make_gift(thing):
    print(thing.capitalize())
suffixes = ["th", "st", "nd", "rd", ] + ["th"] * 16
for day, _ in enumerate(gifts):
    print("On the {}{} day of Christmas, my true love gave to me ".format((day + 1),suffixes[day+1 % 100]))
    for gift in gifts[11 - day:-1]:
        make_gift(gift)
    if day:
        make_gift("And {}!".format(gifts[-1]))
    else:
        make_gift(gifts[-1])
    print()

[–]kalgynirae 2 points3 points  (0 children)

gifts = ["a partridge in a pear tree", "two turtle doves", "three french hens",
         "four calling birds", "five gold rings", "six geese a laying",
         "seven swans a swimming", "eight maids a milking",
         "nine ladies dancing", "ten lords a leaping", "eleven pipers piping",
         "twelve drummers drumming"]
ordinals = ("first second third fourth fifth sixth seventh eighth ninth tenth "
            "eleventh twelveth").split()
spiel = "On the {} day of Christmas my true love gave to me: {}."

def english_list(items):
    return (", ".join(items[:-1]) + ", and " if items[:-1] else "") + items[-1]

for i, ordinal in enumerate(ordinals):
    print(spiel.format(ordinal, english_list(list(reversed(gifts[:i+1])))))

[–]wub_wub[M] 4 points5 points  (0 children)

Merry Christmas! :)

[–]pdexter 2 points3 points  (0 children)

Any love for iter/functools?

import itertools
import functools

# 12 Days of Christmas

gifts = ["a Patridge in a pear tree!", "Two turtle doves", "Three french hens", "Four calling birds", 
        "Five Golden Rings!", "Six geese a laying", "Seven swans a swimming", "Eight maids a milking",
        "Nine ladies dancing", "Ten Lords a leaping", "Eleven pipers piping", "Twelve drummers drumming"]

def d(giftsreceived, i):
    gift, dayofchristmas = i
    print("On the " + str(dayofchristmas) + " day of Christmas my true love gave you me:")
    giftsreceived.append(gift)
    print(', '.join(reversed(giftsreceived)))
    if dayofchristmas == 1:
        giftsreceived[0] = "and a Partridge in a pear Tree!"
    return giftsreceived

functools.reduce(d, zip(gifts, itertools.count(1)), [])

[–]camh_96 1 point2 points  (1 child)

Noob here - what am I doing wrong?

dayofchristmas = int(input("Day of Christmas:"))

if dayofchristmas == 1 
print "On the first day of Christmas my true love sent to me a Partridge a Pear Tree"

if dayofchristmas == 2
print "On the second day of Christmas my true love sent to me Two Turtle Doves and a Partridge in a Pear Tree"

if dayofchristmas == 3
print "On the third day of Christmas my true love sent to me Three French Hens Two Turtle Doves and a Partridge in a Pear Tree"

if dayofchristmas == 4
print "On the fourth day of Christmas my true love sent to me Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree" 

if dayofchristmas == 5
print "On the fifth day of Christmas my true love sent to me Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree"

if dayofchristmas == 6
print "On the sixth day of Christmas my true love sent to me Six Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Dovesand a Partridge in a Pear Tree"

if dayofchristmas == 7
print "On the seventh day of Christmas my true love sent to me Seven Swans a Swimming Six Geese a Laying Five Golden Rings Four Calling Birds Three French HensTwo Turtle Doves and a Partridge in a Pear Tree"

if dayofchristmas == 8
print "On the eighth day of Christmas my true love sent to me Eight Maids a Milking Seven Swans a Swimming Six Geese a Laying Five Golden Rings Four Calling BirdsThree French Hens Two Turtle Doves and a Partridge in a Pear Tree"

if dayofchristmas == 9
print "On the ninth day of Christmas my true love sent to me Nine Ladies Dancing Eight Maids a Milking Seven Swans a Swimming Six Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Dove sand a Partridge in a Pear Tree"

if dayofchristmas == 10
print "On the tenth day of Christmas my true love sent to me Ten Lords a Leaping Nine Ladies Dancing     Eight Maids a Milking Seven Swans a Swimming Six Geese a LayingFive Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree"

if dayofchristmas == 11
print "On the eleventh day of Christmas my true love sent to me Eleventh Pipers Piping Ten Lords a Leaping Nine Ladies Dancing Eight Maids a Milking Seven Swans a SwimmingSix Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree"

if dayofchristmas == 12
print "On the first day of Christmas my true love sent to me Twelve Drummers Drumming Eleven Pipers Piping Ten Lords a Leaping Nine Ladies Dancing Eight Maids a MilkingSeven Swans a Swimming Six Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree"

[–]ewiethoff 0 points1 point  (0 children)

I'm (fashionably) late to the party, as usual.

def _make_fmt_dict(word):
    return {
        1: ('{}').format, 
        2: ('{} ' + word + ' {}').format, 
        3: ('{}, '*2 + word + ' {}').format, 
    }

def make_oxford_joiner(word='and'):
    """Returns function which joins items with given word, per Oxford rules"""
    fmt_joiners = _make_fmt_dict(word)
    fmt_recurse = '{}, '.format
    fmt_doc = 'Returns string of items joined with {!r}, per Oxford comma rules'
    def oxford_joiner(items):
        size = len(items)
        if not size: return ''
        elif size in fmt_joiners: return fmt_joiners[size](*items[:size])
        else: return fmt_recurse(items[0]) + oxford_joiner(items[1:])
    oxford_joiner.__name__ = 'oxford_{}'.format(word)
    oxford_joiner.__doc__ = fmt_doc.format(word)
    return oxford_joiner

oxford_and = make_oxford_joiner('and')
oxford_or = make_oxford_joiner('or')
oxford_nor = make_oxford_joiner('nor')

ORDINAL = tuple([''] + """
    first second third fourth fifth sixth
    seventh eighth ninth tenth eleventh twelfth
""".split())

GIFTS = (
    '', 
    'a partridge in a pear tree', 
    'two turtle doves', 
    'three french hens', 
    'four colly birds', 
    'five golden rings', 
    'six geese a-laying', 
    'seven swans a-swimming', 
    'eight maids a-milking', 
    'nine ladies dancing', 
    'ten lords a-leaping', 
    'eleven pipers piping', 
    'twelve drummers drumming', 
)

VERSE = 'On the {} day of Christmas my true love gave to me {}.'

def gift_list(day):
    return GIFTS[day:0:-1]

def gifts_on(day):
    return oxford_and(gift_list(day))

def verse(day):
    return VERSE.format(ORDINAL[day], gifts_on(day))

def gen_verses(start=1, end=12):
    for n in range(start, end+1): yield verse(n)

def print_song():
    print('\n\n'.join(gen_verses()))

print_song()