concept Christmas in category python

appears as: Christmas
Tiny Python Projects: Learn coding and testing with puzzles and games

This is an excerpt from Manning's book Tiny Python Projects: Learn coding and testing with puzzles and games.

I think we can learn tons from writing fun programs that generate random insults or produce verses to “The Twelve Days of Christmas” or play Tic-Tac-Toe. Some of the programs in this book even dabble a bit in cryptography, like in chapter 4 where we encode all the numbers in a piece of text or in chapter 18 where we create signatures for words by summing the numeric representations of their letters. I hope you’ll find the programs both amusing and challenging.

13 Twelve Days of Christmas: Algorithm design

Perhaps one of the worst songs of all time, and the one that is sure to ruin my Christmas spirit, is “The Twelve Days of Christmas.” WILL IT EVER STOP!? AND WHAT IS WITH ALL THE BIRDS?! Still, it’s pretty interesting to write an algorithm to generate the song starting from any given day because you have to count up as you add each verse (day) and then count down inside the verses (recapitulating the previous days’ gifts). You’ll be able to build on what you learned writing the program for “99 Bottles of Beer.”


Our program in this chapter will be called twelve_days.py, and it will generate the “Twelve Days of Christmas” song up to a given day, specified by the -n or --num argument (default 12). Note that there should be two newlines between verses but only one at the end:

$ ./twelve_days.py -n 3
On the first day of Christmas,
My true love gave to me,
A partridge in a pear tree.
 
On the second day of Christmas,
My true love gave to me,
Two turtle doves,
And a partridge in a pear tree.
 
On the third day of Christmas,
My true love gave to me,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.