CSC111 Lab 6 2014
--D. Thiebaut (talk) 15:00, 3 March 2014 (EST)
Old MacDonald's Farm
Old MacDonald's farm is a song (almost) all American kids learn at one point in their life. I certainly never heard it when I grew up in France, but I think most of you will be familiar with it. If you don't know it, this YouTube video will get you acquainted with it. :-)
The goal of this problem is for you to add a section to the program below so that it prints parts of the lyrics of the famous song using a for-loop and functions.
Here's the beginning program which you have to modify:
# start with four animals in the farm
farm = [ "horse", "pig", "dog", "cat" ]
# sing(): a function that simulates singing a refrain for the song
# using the animal name.
def sing( word ):
print( word )
# "sing" all the names of the animals
for animal in farm:
sing( animal )
- Create the program in lab6.py and run it.
Challenge 1 |
- Modify the function so that the output looks like the lyrics of the song:
And on his farm he had a horse, E-I-E-I-O And on his farm he had a pig, E-I-E-I-O And on his farm he had a dog, E-I-E-I-O And on his farm he had a cat, E-I-E-I-O
- Make sure there's a blank line between each line with an animal name.
Challenge 2 |
- Modify the function some more so that the output looks like the lyrics of the song:
Old MacDonald had a farm, E-I-E-I-O And on his farm he had a horse, E-I-E-I-O Here a horse, there a horse, everywhere a horse! Old MacDonald had a farm, E-I-E-I-O And on his farm he had a pig, E-I-E-I-O Here a pig, there a pig, everywhere a pig! Old MacDonald had a farm, E-I-E-I-O And on his farm he had a dog, E-I-E-I-O Here a dog, there a dog, everywhere a dog! Old MacDonald had a farm, E-I-E-I-O And on his farm he had a cat, E-I-E-I-O Here a cat, there a cat, everywhere a cat!
Challenge 3 |
- Modify your program a third time, and introduce a new function that will receive 4 animals as parameters, and will call the function sing( ) for each one.
- Here is an outline of what your final program should look like (you have to figure out what to replace the ellipses with!)
def sing( word ):
print( ... )
print( ... word ... )
print( ... )
def singSong( animal1, animal2, animal3, animal4 ):
sing( animal1 )
sing( animal2 )
sing( animal3 )
sing( animal4 )
singSong( "horse" ... )