Outputting lyrics

Now, lets do the code to print out the song, line by line. These lines will go in the loop. Lets take a look at the first one. This line of code will *almost* work, but there is a slight caveat. If we run this code as is, we will get an error. Python doesn’t like us trying to add the variables “on_the_wall” and “containers”. So why not?

print(on_the_wall + containers + ” of ” + filling + ” on the wall…”)

Recall that on_the_wall is a number, we set it to 99 earlier. So if we coded on_the_wall + 11, it would give us 110 back. Also recall that “containers” is a string that the user entered, so if they set it to “Buckets” and we coded containers + ” lol”, we would get the string “Buckets lol”. By now you may see the problem. Python knows how to add two strings together, and how to add two numbers together, but it doesn’t know what it should do if you try to add a number to a string, so it gives you an error. It would be like asking somebody “Hey, what’s 17 plus ‘I love you’?”. They’d probably give you a real weird look. To get around this, we can tell python that we want to treat our on_the_wall variable as if it was a string, by using the str() function, which can be used to convert a number to a string.

With this in mind, our print statement should actually look like the following. Anywhere that we want to use our “on the wall” variable, we will need to use the str() function right in front of it.

print(str(on_the_wall) + containers + " of " + filling + " on the wall...")

From here, the rest of the code should be easy, just make sure that it takes place in the loop, the full code would look as follows

containers = input("Please enter in a container: ")
filling = input("Please enter a filling: ")

on_the_wall = 99

while on_the_wall > 0:
    print(str(on_the_wall) + " " + containers + " of " + filling + " on the wall, ")
    print(str(on_the_wall) + " " + containers + " of " + filling)
    print("Take one down,")
    print("Pass it around,")
    on_the_wall = on_the_wall - 1
    print(str(on_the_wall) + " " + containers + " of " + filling + " on the wall... \n")

Give this a run and you will see your code print out all of the lines of the song.