“Ninety-Nine Bottles” is a folk song of undetermined origin known for its length and repetitiveness. The lyrics go, “Ninety-nine bottles of milk on the wall, ninety-nine bottles of milk. Take one down, pass it around, ninety-eight bottles of milk on the wall.” As the lyrics repeat, the number of bottles falls from ninety-eight to ninety-seven, then from ninety-seven to ninety-six, until it reaches zero: “One bottle of milk on the wall, one bottle of milk. Take it down, pass it around, no more bottles of milk on the wall!”
Luckily for us, computers are excellent at performing repetitive tasks, and this program reproduces all of the lyrics programmatically. An extended version of this program is in Project 51, “niNety-nniinE BoOttels.”
The Program in Action
When you run ninetyninebottles.py, the output will look like this:
Ninety-Nine Bottles, by Al Sweigart [email protected]
(Press Ctrl-C to quit.)
99 bottles of milk on the wall,
99 bottles of milk,
Take one down, pass it around,
98 bottles of milk on the wall!
98 bottles of milk on the wall,
98 bottles of milk,
Take one down, pass it around,
97 bottles of milk on the wall!
How It Works
The repetition in this song makes it easy to use a for loop (from lines 20 to 30) to display the first 98 stanzas. However, the last stanza has some minor differences and requires separate code to display (lines 33 to 39). This is because the last line, 'No more bottles of milk on the wall!', deviates from the line repeated in the loop, and because the word “bottle” is singular rather than plural.
1. """Ninety-Nine Bottles of Milk on the Wall 2. By Al Sweigart [email protected] 3. Print the full lyrics to one of the longest songs ever! Press 4. Ctrl-C to stop. 5. This code is available at https://nostarch.com/big-book-small-python-programming 6. Tags: tiny, beginner, scrolling""" 7. 8. importsys,time 9. 10. print('Ninety-Nine Bottles, by Al Sweigart [email protected]')11. print()12. print('(Press Ctrl-C to quit.)')13. 14. time.sleep(2)15. 16. bottles=99# This is the starting number of bottles.17. PAUSE=2# (!) Try changing this to 0 to see the full song at once.18. 19. try:20. whilebottles>1:# Keep looping and display the lyrics.21. print(bottles,'bottles of milk on the wall,')22. time.sleep(PAUSE)# Pause for PAUSE number of seconds.23. print(bottles,'bottles of milk,')24. time.sleep(PAUSE)25. print('Take one down, pass it around,')26. time.sleep(PAUSE)27. bottles=bottles-1# Decrease the number of bottles by one.28. print(bottles,'bottles of milk on the wall!')29. time.sleep(PAUSE)30. print()# Print a newline.31. 32. # Display the last stanza:33. print('1 bottle of milk on the wall,')34. time.sleep(PAUSE)35. print('1 bottle of milk,')36. time.sleep(PAUSE)37. print('Take it down, pass it around,')38. time.sleep(PAUSE)39. print('No more bottles of milk on the wall!')40. exceptKeyboardInterrupt:41. sys.exit()# When Ctrl-C is pressed, end the program.
After entering the source code and running it a few times, try making experimental changes to it. On your own, you can also try to figure out how to do the following:
Create a program for the repetitive song “The Twelve Days of Christmas.”