# ants.py """ Ants go marching function demo for our python class. This prints the lyrics to the song "The ants go marching"; see for example http://kids.niehs.nih.gov/lyrics/antsgo.htm . (However, I've adapted the words a bit to the version I know.) The ants go marching one by one, hurrah, hurrah. The ants go marching one by one, hurrah, hurrah. The ants go marching one by one, The little one stops to suck his thumb And they all go marching down to the ground To get out of the rain, boom, boom, boom, BOOM! The ants go marching two by two, hurrah, hurrah. The ants go marching two by two, hurrah, hurrah. The ants go marching two by two, The little one stops to tie his shoe And they all go marching down to the ground To get out of the rain, boom, boom, boom, BOOM! etc. Notes : * Most functions have a docstring and an explicit test. * Nearly all return strings rather than printing something, which makes the testing easier. Jim Mahoney Oct 6 2008 GPL """ def word_for_number(n): """Return the word for a number from 0 to 10, e.g. 'one' for 1. >>> word_for_number(1) 'one' """ numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'] return numbers[n] def ant_rhyme(n): """Return the rhyming phrase for a given verse. >>> ant_rhyme(7) 'pray to heaven' """ rhymes = ['none', 'suck his thumb', 'tie his shoe', 'climb a tree', 'shut the door', 'take a dive', 'pick up sticks', 'pray to heaven', 'shut the gate', 'check the time', 'say "THE END"', ] return rhymes[n] def ant_line(count, line_ending = "hurrah, hurrah."): """Return one of the first 3 lines of the verse as a string. >>> ant_line(6) 'The ants go marching six by six, hurrah, hurrah.' >>> ant_line(2, "") 'The ants go marching two by two, ' """ return "The ants go marching " \ + word_for_number(count) + " by " \ + word_for_number(count) + ", " + line_ending def ant_verse(count): """Return a verse as a string.""" return ant_line(count) + "\n" \ + ant_line(count) + "\n" \ + ant_line(count, "") + "\n" \ + "The little one stops to " + ant_rhyme(count) + "\n" \ + "And they all to marching down into the ground \n" \ + "To get out of the rain, boom, boom, boom, BOOM! \n" def ant_song(start, end): """Return verses 'start' to 'end' (both from 1 to 10) of the ant song.""" song = "" for count in range(start, end+1): song = song + "\n" + ant_verse(count) return song def _test(): import doctest doctest.testmod() # ------------------------------------------------- _test() print ant_song(1, 10)