0

I'm trying to write a program that displays the lyrics for "Old MacDonald", but when I run the program, I'm getting all of my first function's repeats followed by my second function's repeats. What I'm saying probably doesn't make much sense. Just look at the code.

def verseFor(animal):
    lyrics = "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!\n" + \
    "And on that farm he had a " + animal + ", Ee-igh, Ee-igh, Oh!"
    return lyrics

def verseFor2(noise):
    lyrics2 = "With a " + noise + ", " + noise + " here and a " + noise + ", " + noise + " there. Here a " + noise + ", there a " + noise + ", everywhere a " + noise + ", " + noise + ".\n" + \
    "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!"
    return lyrics2

def main():
    for animal in ["cow", "duck", "sheep", "horse", "dog"]:
        print(verseFor(animal))

    for noise in ["moo", "quack", "baa", "neigh", "woof"]:
        print(verseFor2(noise))

main()
  • Make it one function, [combine the lists into tuples, as explained here](https://stackoverflow.com/questions/2407398/how-to-merge-lists-into-a-list-of-tuples), and call the function on a tuple. – DYZ Jun 07 '20 at 20:57
  • For future reference, a for loop is, well, a loop, and loops through itself before continuing on. – Putnam Jun 07 '20 at 21:00
  • You got a good answer from @ThierryLathuille. I'd like to suggest one improvement. Your code calls for an f-string: `lyrics2 = f"With a {noise}, {noise} here and a {noise}, {noise} there. Here a {noise}, there a {noise}, everywhere a {noise}, {noise}.\nOld MacDonald had a farm, Ee-igh, Ee-igh, Oh!"` – Matthias Jun 07 '20 at 21:10

1 Answers1

3

This is a good use for zip:

def verseFor(animal):
    lyrics = "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!\n" + \
    "And on that farm he had a " + animal + ", Ee-igh, Ee-igh, Oh!"
    return lyrics

def verseFor2(noise):
    lyrics2 = "With a " + noise + ", " + noise + " here and a " + noise + ", " + noise + " there. Here a " + noise + ", there a " + noise + ", everywhere a " + noise + ", " + noise + ".\n" + \
    "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!"
    return lyrics2

def main():
    for animal, noise in zip(["cow", "duck", "sheep", "horse", "dog"],
                             ["moo", "quack", "baa", "neigh", "woof"]):
        print(verseFor(animal))
        print(verseFor2(noise))

main()
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50