Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON ASSIGNMENT old output Old McDonald had a farm, E-I-E-I-O. And on that far

ID: 3577005 • Letter: P

Question

PYTHON ASSIGNMENT

old output

Old McDonald had a farm, E-I-E-I-O.
And on that farm he had a chicken, E-I-E-I-O.
With a cluck-cluck here, and a cluck-cluck there.
Here a cluck, there a cluck, everywhere a cluck-cluck.
Old McDonald had a farm, E-I-E-I-O.

Old McDonald had a farm, E-I-E-I-O.
And on that 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 McDonald had a farm, E-I-E-I-O.

update program:

For the verse function to print out the "real" verses to the song, it's going to have to have a way to "remember" the sounds from the previous verses. Use a list to hold the animal sounds. Since the list will need to be "shared" between different calls of the verse function, the list will need to be global. (The other option would be to have the list local to main, but then it would need to be passed as part of each call to verse.) When you declare the global variable, set its value to be an empty list.

The following code fragment gives an example of declaring the variable sounds and assigning an empty list to it.

sounds = []

--->driver function
def main():

   # print the first three verses
   old_mcdonald.verse('chicken', 'cluck')
   old_mcdonald.verse('cow', 'moo')

main()

--->old_mcdonald.py

def verse(animal, sound):
   pass

update def verse in old_mcdonald to output


Old McDonald had a farm, E-I-E-I-O.
And on that farm he had a chicken, E-I-E-I-O.
With a cluck-cluck here, and a cluck-cluck there.
Here a cluck, there a cluck, everywhere a cluck-cluck.
Old McDonald had a farm, E-I-E-I-O.

Old McDonald had a farm, E-I-E-I-O.
And on that 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.
With a cluck-cluck here, and a cluck-cluck there.
Here a cluck, there a cluck, everywhere a cluck-cluck.
Old McDonald had a farm, E-I-E-I-O.

add a second function to old_mcdonald
--->ask the user to input an animal and sound to add to the list

----> If both the animal and the sound pass validation, print out a blank line, then call the verse function, passing the new animal and sound. Following the call to verse, the query_verse function returns True. If either the animal or the sound fails validation, prompt again, up to five times. After five validation failures for the animal or five failures for the sound, the function returns False, indicating that no verse was added to the song.

Explanation / Answer

#user input

def add_verse():

    animal = input("Enter an animal: ")

    sound = input("Enter that animal's sound: ")

    num=0

    while(num<5):

        num+=1

      if query_verse(animal, sound):   

            print

            verse(animal, sound)

            return True

            break

    return False   

       

#validation

def query_verse(animal, sound):

    return (type(animal)==str and type(sound)==str):

#printing verse

def verse(animal, sound):

    print animal, sound