-1

I'm working on a school assignment and having some trouble. For it, I need to use lists and loops to have a user be able to input animals and sounds into two lists. The loop is then supposed to output the lyrics in a group, using the animal and sound where appropriate one at a time, so something like:

And on this farm he had a dog...etc and going through the song, and the next going And on this far he had a cat...etc through all animals and sounds in the list

Right now it partially works. A user is able to input three animals and three sounds, and the print will then run however it will only output the last entry in the list. I want it to print the lyrics with all entries from the list. I also want the user to be able to cancel out with -1, but everything I've tried has not functioned. If anyone can help that would be great! I've included my code so far below:

for song in range (3):
    animal = input("Please Input an Animal: ")
    sound = input("Please Input a Sound: ")
    
    lyrics = "Old Macdonald had a farm, E- I- E- I- O," "And on that farm he had a %s, E- I- E- I- O." "With a %s - %s here,And a %s - %s there," "Here a %s, there a %s, Everywhere a %s - %s" "Old Macdonald had a farm, E- I- E- I- O!" % (animal, sound, sound, sound, sound, sound, sound, sound, sound)
print(lyrics)

3 Answers3

1

Sunero4 already explained why the animal and sound variables get assigned only the last user entry. I added some formatting to his answer and added in the sys.exit(0) after each user input to check if -1 was entered such that we can exit. Note that we use 'import sys' to call this module. I don't know if you have covered that in your classes yet. If not, I recommend having a look at how your current course notes would deal with the problem.

import sys

lyrics_list = []

for song in range (3):
    animal = input("Please Input an Animal: ")
    if animal == '-1':
        sys.exit(0)
    sound = input("Please Input a Sound: ")
    if sound == '-1':
        sys.exit(0)     
    
    lyrics = "Old Macdonald had a farm, E- I- E- I- O," " And on that farm he had a %s, E- I- E- I- O." " With a %s - %s here, and a %s - %s there," " here a %s, there a %s, everywhere a %s - %s" " Old Macdonald had a farm, E- I- E- I- O!" % (animal, sound, sound, sound, sound, sound, sound, sound, sound)
    lyrics_list.append(lyrics)

for lyric in lyrics_list:
    print(lyric)

Edit: You could replace the sys.exit(0) with quit() and it will do the same. For further details on the difference between sys.exit() and quit() have a look here Python exit commands - why so many and when should each be used?

0

So the reason this is not working as you expect is because you are just reassigning the animal and sound variables, not adding them to a list. To make it work so that the user can input three sounds and animals, you could do something like this:

    lyrics_list = []

    for song in range (3):
        animal = input("Please Input an Animal: ")
        sound = input("Please Input a Sound: ")
         
    
        lyrics = "Old Macdonald had a farm, E- I- E- I- O," "And on that farm he had a %s, E- I- E- I- O." "With a %s - %s here,And a %s - %s there," "Here a %s, there a %s, Everywhere a %s - %s" "Old Macdonald had a farm, E- I- E- I- O!" % (animal, sound, sound, sound, sound, sound, sound, sound, sound)
        lyrics_list.append(lyrics)

    for lyric in lyrics_list:
        print(lyric)
sunero4
  • 820
  • 9
  • 29
0

Try this?

# if length != length2: (code to check the lists are of the same length)

animal_list = ['Dog', 'Cat', 'Horse', 'Goat'] # using this in place of appending user input to an empty list
sound_list = ['Bark', 'Meow', 'Neigh', 'Bleat']
length = len(animal_list) 
# length2 = len(sound_list)    

for x in range(length):
sentence = 'start here ' + animal_list[x] + ' body of song ' + sound_list[x] + ' rest of song'
print(sentence)
Potato
  • 99
  • 5
  • 1
    thank you for your response but this isn't quite what I was looking for! Someone already provided a good solution. Thank you for taking time to answer though, I appreciate it! – neoneclectica Dec 07 '20 at 00:15