# write a function that takes an animal and its sound, # use the given animal and sound and # print the "Old MacDonald had a farm song" # note: an animal is always given # but its sound is not # hint: think about the things that can be optional # practice writing function, with optional arguments # difference between print and return # practice conditional statements # Old MacDonald had a farm # E-I-E-I-O # And on his farm he had a cow # E-I-E-I-O # With a moo-moo here # And a moo-moo there # Here a moo, there a moo # Everywhere a moo-moo # Old MacDonald had a farm # E-I-E-I-O def old_macdonald(animal, sound=None): """ This function takes an animal and its sound and print an Old MacDonald had a farm song :param animal: :param sound: optional (default is None) :return: None """ if sound is None: sound = "????" # is will return True if two variables point to the same object # == if the objects referred to by the variables are equal # Generally, we can use == to compare value # However, in this example, since we set a default value to None # and None is an object, it is common to use is operator to compare # objects print("Old MacDonald had a farm", "E-I-E-I-O", "And on his farm he had a ", animal, "E-I-E-I-O", "With a", sound, "-", sound, "here", "And a", sound, "-", sound, "there", "Here a", sound, ", there a", sound, "Everywhere a", sound, "-", sound, "Old MacDonald had a farm", "E-I-E-I-O") print("=== call old_macdonald ===") old_macdonald("cow", "moo") # old_macdonald("pig", "oink") old_macdonald("pig") print("=== call old_macdonald and print the result ===") # notice that the function return nothing print(old_macdonald("cow", "moo")) print(old_macdonald("pig")) print("=================") # modify the function to return the song instead of print def old_macdonald2(animal, sound=None): """ This function takes an animal and its sound and return an Old MacDonald had a farm song :param animal: :param sound: optional (default is None) :return: string representing old macdonald song """ if sound is None: sound = "????" # is will return True if two variables point to the same object # == if the objects referred to by the variables are equal # Generally, we can use == to compare value # However, in this example, since we set a default value to None # and None is an object, it is common to use is operator to compare # objects # return ("Old MacDonald had a farm", # "E-I-E-I-O", # "And on his farm he had a ", animal, # "E-I-E-I-O", # "With a", sound, "-", sound, "here", # "And a", sound, "-", sound, "there", # "Here a", sound, ", there a", sound, # "Everywhere a", sound, "-", sound, # "Old MacDonald had a farm", # "E-I-E-I-O") # there are many ways to format strings # here is another way to format string, store it in a variable # then return the variable result = """Old MacDonald had a farm E-I-E-I-O And on his farm he had a """ + animal result += " E-I-E-I-O With a " + sound + " " + "-" + " " + sound + " " + "here" result += " And a" + sound + " " + "-" + " " + sound + " " + "there" result += " Here a" + " " + sound + " " + ", there a" + " " + sound + " " \ " Everywhere a" + " " + sound + " " + "-" + " " + sound + " " result += "Old MacDonald had a farm" + " " + "E-I-E-I-O" return result print("=== call old_macdonald2 ===") old_macdonald2("cow", "moo") # old_macdonald("pig", "oink") old_macdonald2("pig") # notice that the function doesn't print anything, it only returns # calling the function does not show anything on screen print("=== call old_macdonald2 and print the result ===") print(old_macdonald2("cow", "moo")) # old_macdonald("pig", "oink") print(old_macdonald2("pig"))