Solution Template
Try to first write a solution from scratch. But if you have difficulty, you can use the following
partial program as a starting place. Copy the following code from https://invpy.com/bottlesofbeer-
Python Programming Exercises, Gently Explained
73
template.py and paste it into your code editor. Replace the underscores with code to make a working
program:
# Loop from 99 to 2, displaying the lyrics to each stanza.
for numberOfBottles in range(99, 1, ____):
print(____, 'bottles of beer on the wall,')
print(____, 'bottles of beer,')
____('Take one down,')
____('Pass it around,')
# If there is only one, print "bottle" instead of "bottles".
if (numberOfBottles - 1) == ____:
____('1 bottle of beer on the wall,')
____:
print(____ - 1, ' bottles of beer on the wall,')
# The last stanza has singular "bottle" and a different final line:
____('1 bottle of beer on the wall,')
____('1 bottle of beer,')
____('Take one down,')
____('Pass it around,')
____('No more bottles of beer on the wall!')
The complete solution for this exercise is given in Appendix A and
https://invpy.com/bottlesofbeer.py. You can view each step of this program as it runs under a debugger at
https://invpy.com/bottlesofbeer-debug/.
Further Reading
Project #50 in my book, The Big Book of Small Python Projects, also implements this ―99 bottles of
beer‖ exercise. You can read it for free online at https://inventwithpython.com/bigbookpython/. Project #51
is a version where the lyrics deteriorate over time with erased letters, swapped letters, and other
drunken typos.
|